Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7499633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:53:38+00:00 2026-05-29T19:53:38+00:00

My function finds in string hex notation (hexadecimal CSS colors) and replaces with the

  • 0

My function finds in string hex notation (hexadecimal CSS colors) and replaces with the short notation.
For example: #000000 can be represented as #000

import re

def to_short_hex (string):
    match = re.findall(r'#[\w\d]{6}\b', string)

    for i in match:
        if not re.findall(r'#' + i[1] + '{6}', i):
            match.pop(match.index(i))

    for i in match:
        string = string.replace(i, i[:-3])

    return string;

to_short_hex('text #FFFFFF text #000000 #08088')

Out:

text #FFF text #000 #08088

Is there any way to optimize my code using list comprehension etc..?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:53:39+00:00Added an answer on May 29, 2026 at 7:53 pm

    This is what re.sub is for! It’s not a great idea to use a regex to find something and then do a further sequence of search-and-replace operations to change it. For one thing, it’s easy to accidentally replace things you didn’t mean to, and for another it does a lot of redundant work.

    Also, you might want to shorten ‘#aaccee’ to ‘#ace’. This example does that too:

    def to_short_hex(s):
        def shorten_match(match):
            hex_string = match.group(0)
            if hex_string[1::2]==hex_string[2::2]:
                return '#'+hex_string[1::2]
            return hex_string
        return re.sub(r"#[\da-fA-F]{6}\b", shorten_match, s)
    

    Explanation

    re.sub can take a function to apply to each match. It receives the match object and returns the string to substitute at that point.

    Slice notation allows you to apply a stride. hex_string[1::2] takes every second character from the string, starting at index 1 and running to the end of the string. hex_string[2::2] takes every second character from the string, starting at index 2 and running to the end. So for the string “#aaccee”, we get “ace” and “ace”, which match. For the string “#123456”, we get “135” and “246”, which don’t match.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Write a function which finds a substring in a string and replaces all such
I can't seem to find a function that captures and returns a regex'ed string.
I have a function that finds a regex thingy, then replaces with php code.
I have a function that finds any ISO 8859-1 symbol within a given string,
I'm writing a trimming function that takes a string and finds the first newline
I'm trying to find a Delphi function that will split an input string into
Anyone know what function or file in linux holds the algorithm that finds a
How can I write an Emacs Lisp function to find all hrefs in an
I'm looking for a function that finds a substring from an array of strings
I have a list of String representations of unicode hex values such as 0x20000

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.