A Regular Expression Primer

This table breifly compares the regular exprssion capability of Perl, grep, egrep, and lex. This table does not include minimal matching, casefolding, and pattern substitution features of the various regular expression languages in each tool. Any pattern matching of lex also works in flex.

charsactionPerlgrepegreplex/flex
abc...Match that character (metacharacters excluded)Perlgrepegreplex/flex
\\\.\*...Match that metacharacter following the back slashPerluses shuses shlex/flex
""De-meta any chars inside quotes   lex/flex
\t,\n,\r,\ftab, newline, return, form feedPerlgrepegreplex/flex
.Match any characterPerlgrepegreplex/flex (not \n)
[]Character classPerlgrepegreplex/flex
[^]Inverse Character classPerlgrepegreplex/flex
[-]Character rangesPerlgrepegreplex/flex
\wMatch a "word" character (alphanumeric plus "_")Perl   
\WMatch a non-word characterPerl   
\sMatch a whitespace characterPerl   
\SMatch a non-whitespace characterPerl   
\dMatch a digit characterPerl   
\DMatch a non-digit characterPerl   
anchorsactionPerlgrepegreplex/flex
^Match the beginning of the linePerlgrepegreplex/flex
$Match the end of the linePerlgrepegreplex/flex
\bMatch a word boundaryPerl   
\BMatch a non-(word boundary)Perl   
operatorsactionPerlgrepegreplex/flex
|AlternationPerl egreplex/flex
()GroupingPerl egreplex/flex
multiplicityactionPerlgrepegreplex/flex
*Greedy match 0 or more timesPerlgrepegreplex/flex
+Greedy match 1 or more timesPerl egreplex/flex
?Greedy match 1 or 0 timesPerlgrepegreplex/flex
{n}Greedy match exactly n timesPerl egrep 
{n,}Greedy match at least n timesPerl egrep 
{n,m}Greedy match at least n but not more than m timesPerl egreplex/flex
lookAheadactionPerlgrepegreplex/flex
/Look ahead predicate   lex/flex
(?= )Look ahead predicatePerl   
(?! )NOT Look ahead predicatePerl   
Greedy means that it trys to find the longest pattern that matches from the point where the previous pattern left off.

Some Examples of Regular Expressions

dog matches the string "dog"
[dog] matches matches one character: a "d" an "o" or a "g"
[dog]* matches matches a string of zero or more characters from the set {"d" an "o" or a "g"}
(dog|cat) matches the string "dog" or the string "cat"
dog.*cat matches the string "dog" followed by the string "cat" somewhere later in the string
x(dog|cat)x matches the string "dog" or the string "cat" between two "x"s
xx* matches a string of one or more "x"s
x+ matches a string of one or more "x"s
x(dog|cat)?x matches two "x"s with optionally the string "dog" or the string "cat" between the "x"'s
[aeiou] matches a single vowel
[A-Z]+ matches a string of one or more uppercase characters
[az-]+ matches a string of one characters from the set or three characters "a", "z", "-"
[^a-z]+ matches a string of one or more characters that are not lowercaase letters
"[a-z]" in flex matches exactly the five character string "[a-z]"
[a-zA-Z][a-zA-Z0-9]* matches a letter optionally followed by letters or digits
[1-9][0-9]*|0 matches a positive integer with no leading zero except when the number is zero
[+-]?[0-9]+ matches an integer with optional sign (note that leading zeroes are allowed
([0-9].)* matches an even number of characters where every odd numbered character is a digit
[+-]?[1-9][0-9]*|0 matches an integer with no leading zero except when the number is zero. The number may have an optional sign
[\^\+\-\:\*\]] matches one of the 6 characters: "^", "+", "-", ":", "*", "]"