Meta Characters that need to be escaped with a \ :
. ^ $ * + ? { } [ ] \ | ( )
Insert Mode | |
. | - Any Character Except New Line |
\d | - Digit (0-9) |
\D | - Not a Digit (0-9) |
\w | - Word Character (a-z, A-Z, 0-9, _ ) |
\W | - Not a Word Character |
\s | - Whitespace (space, tab, newline) |
\S | - Not a Whitespace |
\w | - Word Boundary |
^ | - Beginning of a String |
$ | - End of a String |
[ ] | - Matches Any Character in the Brackets |
[^ ] | - Matches Any Character NOT in the Brackets |
( ) | - Matches Groups of Characters |
| | - Either OR - used with Groups |
Quantifiers: | |
* | - 0 or More |
+ | - 1 or More |
? | - 0 or 1 |
{3} | - Exact Number |
{3,4} | - Range of Numbers (Min,Max) |
Most Linux distributions use the gnu version of grep (global regular expression print). The gnu version of grep understands three differtnt types of regular expression syntax:
1) grep (basic regular Expressions)
2) grep -E (Extended regular Expressions)
3) grep -P (perl regular expressions).
Only the -P options matches everything in the tables.
The basic and extended do not support \d, \D, etc.
The basic does not support {3}, but the extended does support it.
Examples:
$ cat grep_test.txt
123.456.7890
012*346*6789
800-124-4567
Mr. T
Mr Ray
Mrs. Raymond
Mrs Jones
Ms. Smith
Ms Wilson
$ grep -P '\d{3}[.-]\d{3}[-.]\d{4}' grep_test.txt
123.456.7890
800-123-4567
$ grep -P '(Mr.|Mr|Mrs.|Mrs|Ms.|Ms)\s[A-Z]' grep_test.txt
Mr. T
Mr Ray
Mrs. Raymond
Mrs Jones
Ms. Smith
Ms Wilson
$ grep -P 'ay$' grep_test.txt
Mr. Ray