Linux Command Reference ยท Concepts
BRE, ERE and PCRE regular expressions
Compare the regular expression flavors on Linux: POSIX basic (BRE), extended (ERE) and Perl-compatible (PCRE), and which tools use which, with side-by-side examples.
"Regular expression" is a family of related languages, not one language. On Linux you meet three main flavors: BRE (basic regular expressions, the POSIX default), ERE (extended, a friendlier POSIX syntax) and PCRE (Perl-compatible, the dialect of most programming languages). A pattern that works in one may mean something else, or nothing, in another.
Always ask "which flavor does this tool use?" before writing a pattern. The table below answers that for the common tools.
grep 'a\+b' file # BRE (GNU escapes) grep -E 'a+b' file # ERE grep -P '\d+' file # PCRE (GNU grep built with it)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Which tool uses which
| Tool | Default | How to change |
|---|---|---|
grep |
BRE | -E ERE, -P PCRE (if compiled in), -F fixed strings |
sed |
BRE | -E or -r for ERE |
awk |
ERE | n/a |
expr (: operator) |
BRE, anchored at start | n/a |
vim |
its own (close to BRE) | \v "very magic" for ERE-like |
find -regex |
emacs-style | -regextype posix-extended |
Bash [[ =~ ]] |
ERE | n/a |
less, man search |
system regex (BRE-ish) | n/a |
Python, JavaScript, Java, PHP preg_* |
PCRE-like | n/a |
Comparing the flavors
| Feature | BRE | ERE | PCRE |
|---|---|---|---|
| Any character | . |
. |
. |
| Classes | [[:alpha:]] |
[[:alpha:]] |
\w, \d, \s, also [[:alpha:]] |
| One or more | \+ (GNU) |
+ |
+ |
| Optional | \? (GNU) |
? |
? |
| Alternation | | (GNU) |
| |
| |
| Grouping | \( \) |
( ) |
( ), non-capturing (?: ) |
| Interval | \{n,m\} |
{n,m} |
{n,m} |
| Backreference | \1 |
\1 (GNU) |
\1, named groups |
| Lazy quantifiers | no | no | *?, +? |
| Lookahead / lookbehind | no | no | (?=x), (?<=x) |
| Word boundary | \<, \>, \b (GNU) |
same (GNU) | \b |
In BRE, * at the very start of a pattern or group is a literal star, and ^/$ are anchors only at the edges. In ERE they are always special.
Usage notes
- Rule of thumb: use ERE (
-E) for shell work. It avoids the pile of backslashes of BRE and is portable acrossgrep,sedandawk. egrepandfgrepare the old names forgrep -Eandgrep -F; seegrep -Eandgrep -F.- POSIX leftmost-longest vs Perl leftmost-first: for
a|abonab, BRE/ERE engines prefer the longer matchab; PCRE takes the first alternative,a. This only matters when you extract text. \d,\w,\sare not POSIX. GNU grep accepts\wand\sin BRE and ERE as extensions, but not\d. Use[[:digit:]].- Locale matters: ranges like
[a-z]can behave differently in non-C locales; classes like[[:lower:]]are the safe choice. - Greedy only: POSIX has no lazy quantifier. Emulate it with a negated class, for example
"[^"]*"instead of".*?". - Escaping differs: inside single quotes the shell does not touch backslashes, so write patterns in single quotes.
- Performance: BRE and ERE engines run in linear time and have no catastrophic backtracking; PCRE can backtrack exponentially on bad patterns.