Search code examples
regexsedcygwincharacter-class

Cygwin Regex Shorthand Character Classes Fail to Match


I have a file

cat /tmp/b
A
Quick
Brown
Fox
Killed
2 Hens 

If I match on [0-9] then I get the expected

cat /tmp/b | grep "[0-9]"
2 Hens

But if I try to match on the digit character class I get the unexpected

cat /tmp/b | grep "\d"
Killed

This continues irrespective of what combination of things that I think might work I try

cat /tmp/b | grep "\\d"
Killed
cat /tmp/b | grep "\\d"
Killed
cat /tmp/b | grep "\\\\d"
{No matches}
cat /tmp/b | grep "\\\d"
{No matches}
cat /tmp/b | grep "[\\d]"
Killed
cat /tmp/b | grep "[d]"
Killed
cat /tmp/b | grep "[\\\\d]"
Killed   

Does anyone know what I am doing wrong and how to get Character Classed working in cygwin sed

Update

cat /tmp/b | grep [:digit]
Quick
Killed

Update 2

cat /tmp/b | grep [[:digit:]]
2 Hens

Solution

  • You are specifying the character class incorrectly. The manual would tell you:

    • A bracket expression is a list of characters enclosed by [ and ]
    • Certain named classes of characters are predefined within bracket expressions:
      • [:digit:] Digits: 0 1 2 3 4 5 6 7 8 9.

    This would imply that in order to match a [:digit:] you need to say [[:digit:]].

    You'll find:

    $ cat input | grep -o '[[:digit:]]'
    2