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.
I think that what you need is either one of the following solutions or the other
result
Edit 1
result
Edit 2
result
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 ??