$_ CLI In Depth
Learning progress 0%
lesson text-processing regex awk

Regular Expressions: BRE vs ERE

6 min read Module 6: Text Processing: grep, sed & awk Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:23

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.

BASH1 lines
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.

BASH2 lines
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.

BASH2 lines
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.

Note

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.

BASH2 lines
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.

BASH3 lines
printf 'olor\ncolour\ncolr\n'> words.txt
grep 'olou\?r'words.txt
grep -E 'olou?r'words.txt

The 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.

Matching a regex one position at a time

Visualizer

grep tries a match at each position, leftmost first, and the pattern decides how far the match runs. BRE and ERE differ mainly in which characters need a backslash.

1 / 5
p
o
r
t
␠
8
0
8
0
␠
o
p
e
n
pattern
[0-9]+
meaning
one or more digits

grep tries a match at position 0, then 1, and so on. It reports the leftmost position where a match succeeds, and at that position the longest one.

echo 'port 8080 open' | grep -o -E '[0-9]+'
Terminal lab · 2 steps · real Linux in your browser

Match with BRE and ERE

Run one pattern in each dialect and save the results to files in /workspace.

01
Match with a BRE star
The file words.txt already exists. In BRE, the star means zero or more of the previous character. Save the lines that match ca*t: ``bash grep 'ca*t' words.txt > bre.txt `` Then bre.txt should contain cat, caat, and caaat.
Show hint for step 1
Use the pattern exactly as written, in single quotes, and redirect the output with >.
02
Use ERE for an optional letter
The file colors.txt contains color, colour, and colr. Pass -E so that the question mark is an operator, and save the matches: ``bash grep -E 'colou?r' colors.txt > ere.txt `` Without -E the question mark is a literal and nothing matches.
Show hint for step 2
The -E flag switches grep to extended regular expressions.
Check yourself Question 1 of 4
Concept Check Ordinary character in BRE recall

In a basic regular expression (BRE) without any backslash, which character is an ordinary literal rather than an operator?

Recap & Key Takeaways

BRE treats the grouping, interval, plus, question mark, and alternation symbols as literals unless you escape them with a backslash.
ERE makes those operators special by default, and grep -E, sed -E, and awk all use ERE-style operators when you ask for them.
The dot, star, caret, dollar sign, and bracket expressions work the same in both dialects, and named classes such as [[:digit:]] are the portable choice.
When a pattern behaves oddly, check the dialect first: a missing -E or an unexpected backslash explains most surprises.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue