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 6318081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:36:01+00:00 2026-05-24T15:36:01+00:00

Basically, I am changing any and all hexadecimal values with a blue hue to

  • 0

Basically, I am changing any and all hexadecimal values with a blue hue to its red hue counterpart in a given stylesheet (i.e. #00f is changed to #ff0000 (my function outputs six character hexadecimal values excluding the #)).

It was not a problem creating a regular expression to match hexadecimal colors (I’m not concerned about HTML color names although I may eventually care about rgb, rgba, hsb, etc. values.). This is what I ended up with #(([0-9A-z]{3}){1,2}). It works but I want it to be full proof. For example, if somebody happens to set a background image with a fragment (i.e. #top) with a valid hexadecimal value, I don’t want to change it. I tried doing a negative lookbehind, but it doesn’t seem to work. I was using \B#(([0-9A-z]{3}){1,2}) but if there is a word boundary (such as a space) before the ‘#’, it match the URL fragment. This is what I thought should do the trick but doesn’t: (?<!url\([^#)]*)#(([0-9A-z]{3}){1,2}).

I am using the desktop version of RegExr to test with the following stylesheet:

body {
    background: #f09 url('images#06F');
}
span {
    background=#00f url('images#889');
}
div {
    background:#E4aaa0 url('images#889');
}
h1 {
    background: #fff #dddddd;
}

Whenever, I hover over the (?<! substring, RegExr identifies it as a “Negative lookahead matching ‘url\([^#)]*‘.” Could there be a bug or am I just having a bad regex day? And while we’re at it, are there any other contextes in which a ‘#’ is used for non-hexadecimal purposes?

EDIT: Alright, I can’t program early in the morning. That hexadecimal regex should be #(([0-9A-Fa-f]{3}){1,2})

EDIT 2: Alright, so I missed the detail that most languages require static length lookbehinds.

  • 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-24T15:36:02+00:00Added an answer on May 24, 2026 at 3:36 pm

    I think that what you need is either one of the following solutions or the other

    ss = '''    background: #f09 url('images#06F'); 
        background=#00f url('images #889'); 
        background:#E4aaa0 url('images#890'); 
        background: #fff #dddddd; '''
    
    print ss
    import re
    
    three = '(?:[0-9A-Fa-f]{3})'
    
    regx = re.compile('^ *background[ =:]*#(%s{1,2})' % three,re.MULTILINE)
    print regx.findall(ss)
    
    print '-----------------------------------------------------'
    
    regx = re.compile('(?:(?:^ *background[ =:]*)|(?:(?<=#%s)|(?<=#%s%s)) +)'
                      '#(%s{1,2})' % (three,three,three,three),
                      re.MULTILINE)
    print regx.findall(ss)
    

    result

        background: #f09 url('images#06F'); 
        background=#00f url('images #889'); 
        background:#E4aaa0 url('images#890'); 
        background: #fff #dddddd; 
    ['f09', '00f', 'E4aaa0', 'fff']
    -----------------------------------------------------
    ['f09', '00f', 'E4aaa0', 'fff', 'dddddd']
    

    Edit 1

    ss = '''    background: #f09 url('images#06F'); 
        background=#00f url('images #889'); 
        color:#E4aaa0 url('images#890'); 
        background: #fff #dddddd#125e88    #ae3;
        Walter (Elias) Disney: #f51f51 '''
    
    print ss+'\n'
    
    import re
    
    three = '(?:[0-9A-Fa-f]{3})'
    
    regx = re.compile('^ *[^=:]+[ =:]*#(%s{1,2})' % three,re.MULTILINE)
    print regx.findall(ss)
    
    print '-----------------------------------------------------'
    
    regx = re.compile('(?:(?:^ *[^=:]+[ =:]*)|(?:(?<=#%s)|(?<=#%s%s)) *)'
                      '#(%s{1,2})' % (three,three,three,three),
                      re.MULTILINE)
    print regx.findall(ss)
    

    result

        background: #f09 url('images#06F'); 
        background=#00f url('images #889'); 
        color:#E4aaa0 url('images#890'); 
        background: #fff #dddddd#125e88    #ae3;
        Walter (Elias) Disney: #f51f51 
    
    ['f09', '00f', 'E4aaa0', 'fff', 'f51f51']
    -----------------------------------------------------
    ['f09', '00f', 'E4aaa0', 'fff', 'dddddd', '125e88', 'ae3', 'f51f51']
    

    Edit 2

    ss = '''    background: #f09 url('images#06F'); 
        background=#00f url('images #889'); 
        color:#E4aaa0 url('images#890'); 
        background: #fff #dddddd#125e88    #ae3;
        Walter (Elias) Disney: #f51f51
        background: -webkit-gradient(linear, from(#000000), to(#ffffff));. '''
    
    print ss+'\n'
    
    import re
    
    three = '(?:[0-9A-Fa-f]{3})'
    
    preceding = ('(?:(?:^[^#]*)'
                     '|'
                     '(?:(?<=#%s)'
                         '|'
                         '(?<=#%s%s)'
                         '|'
                         '(?<= to\()'
                         ')'
                     ')') % (three,three,three)
    
    regx = re.compile('%s *#(%s{1,2})' % (preceding,three), re.MULTILINE)
    print regx.findall(ss)
    

    result

        background: #f09 url('images#06F'); 
        background=#00f url('images #889'); 
        color:#E4aaa0 url('images#890'); 
        background: #fff #dddddd#125e88    #ae3;
        Walter (Elias) Disney: #f51f51
        background: -webkit-gradient(linear, from(#000000), to(#ffffff));. 
    
    ['f09', '00f', 'E4aaa0', 'fff', 'dddddd', '125e88', 'ae3', 'f51f51', '000000', 'ffffff']
    

    Regexes are extremely powerful in the condition that there must be enough portions of strings following a certain organisation having relative stability among variable other portions that are intended to be catched. If the analyzed text becomes too loose in its structure, it becomes impossible to write a regex.

    Are there still a lot of other “Harlequin-like patchwork” structures possible for your strings ??

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

Sidebar

Related Questions

How can i change the look of the jqgrid without changeing the stylesheet. Basically
Basically, something better than this: <input type=file name=myfile size=50> First of all, the browse
Basically the problem I have is that in Safari/Chrome it is changing the width
i have some difficulties changing tab bar controllers. Basically I have UITabBarController with 3
Hello all and thanks for any help in advance. I have a ruby on
Is there any way I can pause all UI Update commands in Winforms? Or
Basically what I want to do it this: a pdb file contains a location
Basically I have some code to check a specific directory to see if an
Basically I'm going to go a bit broad here and ask a few questions
Basically, I would like a brief explanation of how I can access a SQL

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.