Search code examples
regexcygwincurly-brackets

Aren't curly brackets the metacharacters in regular expressions in cygwin?


I want to search the phone-number in a text file using grep in cygwin. The number is 0570-2770521, and I use the regular expression "[0-9]{4}-[0-9]{7}" with total command as grep "[0-9]{4}-[0-9]{7}" ./list.txt, but it didn't work. Then I changed to grep "[0-9]\{4\}-[0-9]\{7\}" ./list.txt, it works!

But since the {} are metacharacters, if escape them, they will be just literal characters, then how can they represent the match times of [0-9]?

Does that I got a wrong understanding? Hope someone can help to explain the confusion, thanks in advance!

Best regards!


Solution

  • man re_format:

    Obsolete ("basic") regular expressions differ in several respects. [...] The delimiters for bounds are \{ and \}, with { and } by themselves ordinary characters.

    Use egrep (or, equivalently, grep -E) for enhanced regular expressions, which may be more familiar to you.