regex

Regex for matching grey colors in hexadecimal notation


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:


Solution

  • You can use thie regex:

    #([0-9A-Fa-f])([0-9A-Fa-f])((?=\2)\1|(?:\1\2){2})\b
    

    RegEx Demo

    This should work on PCRE or on Python/Java/Javascript as well.

    RegEx Details: