Why there are two dialects
Regular expressions were standardized in the POSIX utilities long before most people used them daily. The original design made the basic operators, such as grouping and repetition, plain characters that needed a backslash to become special. Later, the extended dialect made those operators special by default, because they are used far more often than the literal parentheses and braces they would otherwise conflict with. Both dialects still exist. grep uses BRE unless you pass -E, sed uses BRE unless you pass -E or -r, and awk always uses ERE.
A regex engine reads a pattern and decides, for each position in a line, whether the pattern can match starting there. The engine does not look for the whole pattern at once. It walks the text and tries the pattern at every starting point. For most lines this is fast enough that you never think about it, but the rules for reading the pattern are exact, so you need them.
Literal characters and the shared operators
Most characters match themselves. The letters c, a, and t in a pattern match those letters in the text. Four characters have a special meaning in both dialects. The dot . matches any single character. The star * means zero or more of the item before it. The caret ^ anchors the match to the start of a line, and the dollar sign $ anchors it to the end. Square brackets [ ] form a bracket expression that matches one character from a set.
printf 'at\ncaat\ncaaat\ndog\n'| grep 'a*t'This prints cat, caat, and caaat. The star applies to the single a before it, so a* accepts zero, one, or many a characters. The line dog has no c, so it does not match.
BRE: the operators need a backslash
In a basic regular expression, the grouping parentheses, the interval braces, the plus sign, the question mark, and the alternation bar are ordinary characters. To use them as operators you write a backslash in front of them. GNU grep and GNU sed accept \+, \?, and \| as extensions in BRE, but the portable forms are \{m,n\} for intervals and \(...\) for groups.
printf '+b\naab\n'| grep '+b'
printf 'bab\nab\n'| grep -x '(ab\)\{2\}'The first command prints a+b only, because in BRE the plus sign is an ordinary character. The second prints abab, because \(ab\)\{2\} is the group ab repeated exactly twice, and -x requires the whole line to match.
ERE: the operators are plain
In an extended regular expression, the operators +, ?, |, (, ), {m,n} are special without a backslash. To match a literal one you escape it. The same two examples become shorter in ERE.
printf '+b\naab\n'| grep -E '\+b'
printf 'bab\nab\n'| grep -Ex 'ab){2}'The first line now needs a\+b to match a literal plus sign, and it prints only a+b. The second line prints abab. Keeping both dialects in mind is a common source of errors, because a pattern copied from one tool into another can silently change meaning. The safest habit is to pass -E explicitly whenever you use an operator, and to read the pattern as if the other dialect were in effect.
GNU tools also define shortcuts such as \w for a word character, \b for a word boundary, and \< and \> for word starts and ends. They are not in every POSIX implementation, so use them only when you know the tool is GNU.
Anchors, classes, and alternation
Anchors match positions rather than characters. ^cat matches a line that begins with cat, and cat$ matches a line that ends with it. Bracket expressions accept ranges such as [0-9] and named classes such as [[:digit:]], [[:space:]], and [[:upper:]]. Named classes are safer than ranges, because a range like [a-z] depends on the locale in some setups.
printf 'at\nscat\ncats\n'| grep 'cat'
printf 'at\nscat\ncats\n'| grep 'at$'The first command prints cat and cats. The second prints cat and scat. Alternation is the last operator to know. In ERE you write cat|dog. In BRE, GNU grep accepts cat\|dog, which is why a pattern that works in one tool may fail in another.
Reading a pattern piece by piece
A long pattern is easier to trust when you read it in pieces. Start with the anchors, because they define where a match may begin and end. Then read each bracket expression as one unit, since everything inside the brackets is a set of characters and the brackets themselves end at the first closing bracket. After that, look at the operators that apply to the item just before them. A star or an interval always attaches to the closest item, which is a single character, a bracket expression, or a parenthesized group.
Consider the pattern ^[0-9]+ ERROR$ in ERE. The caret requires a match at the start of the line, the bracket expression and the plus sign describe one or more digits, the space and the word ERROR are literal, and the dollar sign requires the match to reach the end of the line. Reading it this way tells you that a line such as 42 ERROR matches, while x 42 ERROR and 42 ERROR now do not.
Every match a regex finds is the leftmost match, and among matches that start at the same position the longest one wins. This is the POSIX rule, and it explains why sed and grep -o can print a longer piece of text than you expected when the pattern contains a star. When a result looks surprising, test the pattern on a short input and add one character at a time until the behavior makes sense.
Hands-on: one pattern, two dialects
Run the commands below in your lab home directory. Each one prints the same two lines, and the point is to see which symbols need the -E flag to behave as operators.
printf 'olor\ncolour\ncolr\n'> words.txt
grep 'olou\?r'words.txt
grep -E 'olou?r'words.txtThe BRE version uses \?, which GNU grep supports, and the ERE version writes the question mark plainly. Both print color and colour. Try changing -E out and watch the second command stop matching colour. You will see the same effect in every tool in this module.