I've an XML file containing (among a lot of other stuff) hexadecimal color codes. I want to inspect all such codes that result in shades of grey. The pattern is
So it should match #eee, #EEEEEE, #333333 but not #00006d, #123456 and so on. Regarding the regex flavor, it should ideally work in Notepad++, if that's no option then Python 2.7 is an alternative.
I tried using the backreference operator I found here. So far, my best attempt is
#([0-9a-fA-F])\1{3}[^0-9a-fA-F]
but I'm having some troubles:
{3}
by {2}
for matching exactly three repetitions but I don't see why{3,6}
should match 3, 4, 5 or 6 repetitions but how can I exclude 4 and 5 repetitions? I thought about #([0-9a-fA-F])\1{3}[^0-9a-fA-F]|#([0-9a-fA-F])\1{6}[^0-9a-fA-F]
but there must be a less ugly syntax for that, right?You can use thie regex:
#([0-9A-Fa-f])([0-9A-Fa-f])((?=\2)\1|(?:\1\2){2})\b
This should work on PCRE or on Python/Java/Javascript as well.
RegEx Details:
#
: Match a #
([0-9A-Fa-f])
: capture group #1 to capture any one hex character([0-9A-Fa-f])
: capture group #2 to capture any one hex character(
: Start capture group #3
(?=\2)
: Make sure next character is same as group #2\1
: Match same character as in group #1|
: OR(?:\1\2){2}
: Match 2 characters as in group #1 then group #2. Repeat this group twice.)
: End of capture group #3\b
: Word boundary