0

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!

cmjauto
  • 19
  • 6

1 Answers1

1

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.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • so, {} is ordinary characters in basic regex, but seems [] works in basic regex, why {} is so special and are there any metacharacters also ordinary in basic regex? I have read many materials, they all didn't mention about this. – cmjauto Jul 09 '15 at 02:55
  • 1
    Because originally the count modifiers were not a part of regexp, they were added later (unlike character classes, which have been there pretty much forever). So to preserve backwards compatibility, they couldn't suddenly promote `{` and `}` from being ordinary characters to metacharacters, because the older scripts that used them as normal characters would break. – Amadan Jul 09 '15 at 02:59