The following command is used to search for a 7-digit phone number:
grep "[[:digit:]]\{3\}[ -]\?[[:digit:]]\{4\}" file
What does \? stand for?
|
The following command is used to search for a 7-digit phone number:
What does |
||||
|
|
|
It's like In your example, the So any of these will match:
The reason it's written as The original version of So that GNU grep could have the zero or one functionality, they added it, but had to use the Note that grep has an
...
...
...
Further info: |
||||
|
|
Unfortunately, the exact syntax of regular expressions varies slightly between different programs: grep regexes aren't exactly the same as sed regexes, which aren't exactly the same as Emacs regexes, which aren't exactly the same as C++ regexes, and so on. To make matters worse, even a "standard" tool like grep can vary slightly between different Unix-like operating systems. In a regex, some characters have special meaning (such as the square brackets in your example), and revert to their normal meaning as literal characters when you "escape" them by putting a backslash in front of them (so a literal bracket would be written as \[). Others work the other way around, and only take on special meaning when escaped (e.g. plain n is just a letter, but \n is a line feed). And these, again, can vary between regex implementations. In most regex implementations, a question mark means that the previous item is optional, while an escaped question mark (\?) is a literal question mark. But in a few dialects, it's the other way around. Your example could make sense either way around, but I suspect you have one of the dialects where ? is a literal and \? is the optional symbol. So your regex probably means "three digits, optionally followed by a space or dash, followed by four digits". (Another clue can be seen in constructs like \{3\}, which is clearly intended to mean "exactly 3 of the previous item". In most regex dialects this would be written {3}, and \{ would be a literal brace.) |
|||
|
|
|
This is a quick summary of information that's already contained in the other answers. In In This applies to GNU grep; the details for non-GNU grep implementations may differ slightly. In particular, |
|||
|
|