Regex on the command line
grep, sed, pcre2grep, ripgrep and Perl speak different regex dialects. See BRE vs ERE, lazy quantifiers and lookaround compared on macOS.
Flavors and the common coreCapturing groups and numberingReplacing with regex
Regex on the command line
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- There is no single command-line regex. Basic grep and sed speak POSIX BRE, grep -E and sed -E speak ERE, pcre2grep and grep -P speak PCRE, ripgrep speaks Rust regex, and Perl speaks Perl. Check which one your command is using before trusting a pattern.
- In BRE the operators + ? | ( ) { } are literal unless backslashed, and in ERE they are operators. On macOS, grep 'a+' searched for the text a+ while grep -E 'a+' matched aaa.
- The same pattern behaves differently by tool. On this Mac, grep -E accepted the lazy .*? and \d, but sed -E rejected .*? and silently treated \d as no match. pcre2grep, Perl and ripgrep with -P had all of PCRE.
- POSIX engines return the longest match for an alternation, Perl-style engines the first alternative that works. On foobar, grep -oE 'foo|foobar' printed foobar, while pcre2grep, Perl, ripgrep, Python and JavaScript printed foo.
- Lookaround and \K need PCRE: pcre2grep, grep -P where it exists, rg -P, or Perl. BSD grep has no -P, and plain rg refuses lookaround with an error that suggests --pcre2.
The tools are not all running the same regex
On the command line, you type a pattern in single quotes and press return, and it feels as if there is one regex language. There isn’t. The tools were written at different times by different people, and each carries its own idea of what +, |, \d and ? mean. A pattern copied from a JavaScript answer can silently match nothing, or something different.
The grep lesson and the sed lesson cover those commands’ flags. This one covers the regex side, using what I ran on macOS with BSD grep and sed, pcre2grep 10.49, ripgrep 14.1.1 and Perl 5.34.
Dialect one: basic and extended
POSIX defines two syntaxes. In basic (BRE, the default for grep and sed), + ? | ( ) { } are ordinary characters, and you backslash them to get the operators. In extended (ERE, grep -E, sed -E), they are operators with no backslash.
$ echo 'aaa' | grep -o 'a+' # (no output, exit 1)
$ echo 'aaa+' | grep -o 'a+'
a+
$ echo 'aaa' | grep -oE 'a+'
aaa
$ echo 'cat dog' | grep -o 'cat\|dog' # BSD grep accepted \| in basic mode
cat
dog
$ echo 'aa{2}' | grep -o 'a{2}'
a{2}
$ echo 'aaa' | grep -o 'a\{2\}'
aa
So a+ in basic mode meant “a, then a plus sign”. Backslashed \| and \+ worked on this BSD grep, which is a GNU-style extension to POSIX rather than part of it, so don’t rely on them in a portable script. Prefer -E and write the pattern as you would elsewhere.
Dialect two: what each tool also accepts
Beyond the POSIX core, each tool adds things, and my results were not what I expected:
grep -E sed -E pcre2grep perl rg
\d works no match works works works
\w \s \b works no match works works works
lazy .*? lazy error lazy lazy lazy
(?:...) non-capturing works error works works works
lookahead (?=...) error error works works error, use -P
The surprise was on the left. Apple’s grep -E accepted \d, \w, \s, \b, a lazy .*?, (?:...) and (?i) (echo cat | grep -E '(?i)CAT' matched). Apple’s sed -E rejected .*? and (?: with RE error: repetition-operator operand invalid, and s/\d+/N/ didn’t error. It simply never matched. These are the results on this machine. GNU grep and sed, which most Linux boxes use, may differ, and I did not run them.
The lazy-quantifier case shows the effect. On <b>x</b><i>y</i>:
grep -oE '<.*?>' <b> </b> <i> </i> (lazy here)
grep -oE '<[^>]*>' the same four tags
pcre2grep -o '<.*?>' the same four tags
sed -E 's/<.*?>/_/g' sed: RE error: repetition-operator operand invalid
sed -E 's/<[^>]*>/_/g' _x__y_
Only the negated class, <[^>]*>, worked in every tool, as the greedy-and-lazy lesson would suggest.
Dialect three: leftmost-longest vs first-that-works
POSIX regex has a rule that Perl-style engines don’t. An alternation takes the longest match at the leftmost position. Perl-style engines take the first alternative that succeeds. On foobar:
grep -oE 'foo|foobar' foobar
sed -E 's/foo|foobar/X/' X (the whole word was replaced)
pcre2grep -o 'foo|foobar' foo
perl: foo
rg: foo
node and python3: foo
Same pattern, same text, different match. With POSIX tools the order of alternatives doesn’t matter. With PCRE, Perl, Python and JavaScript, it does, which the alternation lesson explained.
Getting at captures from the shell
Each tool exposes groups in its own way:
echo 'user=ada id=42' | pcre2grep -o1 'id=(\d+)' 42
echo 'user=ada id=42' | rg -o 'id=(\d+)' -r '$1' 42
echo 'ada@example.com' | sed -E 's/([a-z]+)@([a-z.]+)/\2 at \1/' example.com at ada
echo 'user=ada id=42' | pcre2grep -o 'user=\K\w+' ada
pcre2grep -o1 prints group 1. -o1 -o2 printed both, run together as ada42. rg -r '$1' replaces the match with the group, using $1. sed uses \1 in the replacement. \K in PCRE says “forget what I matched so far”, so only ada was reported without a lookbehind.
Lookaround and the PCRE escape hatch
For lookaround, you need a PCRE-family tool. pcre2grep -o '(?<=\$)\d+' printed 10 on price $10. Plain rg refused: look-around, including look-ahead and look-behind, is not supported, and its error suggests --pcre2. rg -P worked and printed 10. BSD grep -P doesn’t exist. grep: invalid option -- P. Perl one-liners fill the gap on any machine that has Perl: perl -nle 'print $& while /\d+/g' printed 1 and 22 for a1 b22.
One rule for scripts: write the pattern for the oldest, plainest tool that has to run it. If it needs lookaround or lazy quantifiers, name the tool in the script, such as pcre2grep, instead of hoping the next machine’s grep has -P.
About the video
The embedded video is PacketHammer’s “Grep Bootcamp 17: grep -E | Unlock Extended Regex”, published 21 November 2025 and 5 minutes 42 seconds long. The description says it introduces -E, shows how extended regex differs from basic regex, and explains why it is useful, with a sample file to try. It has no chapters, and I couldn’t read a transcript, so I can’t say which operators it demonstrates. It covers only grep -E: sed, pcre2grep, rg, lazy quantifiers and leftmost-longest matching are my own runs above. Under “deeper”, RobertElderSoftware’s 13-minute video on BRE, ERE and Perl modes in grep is a longer treatment, but I couldn’t tell from its chapters which grep quirk it chases, and CodeLucky’s seven-minute tour covers grep, sed and awk basics. English per titles and descriptions.
What I did not run
GNU grep and sed (including grep -P), egrep, the video’s quirk, and awk.
Next
The last lesson in this group is about the editor you already have open, and about the case where the pattern comes from a user: find and replace in editors and building patterns from input.
Check yourself
Why does grep 'a+' find nothing in aaa on macOS, while grep -E 'a+' matches?
Plain grep uses basic syntax, where + is an ordinary character, so it searched for the text a+. -E selects extended syntax, where + means one or more. In basic mode some implementations accept a\+ as a GNU-style extension; BSD grep accepted it in my test.
You want the first HTML tag in <b>x</b>, using grep -o. Which patterns worked on macOS, and which tool failed?
grep -oE '<[^>]*>' and pcre2grep -o '<.*?>' both worked, and so did grep -oE '<.*?>' on macOS. sed -E 's/<.*?>/_/g' failed with 'repetition-operator operand invalid', so the negated class is the portable form.
How do you print only the number after id= using pcre2grep, ripgrep, and sed?
pcre2grep -o1 'id=(\d+)', rg -o 'id=(\d+)' -r '$1', and sed -E 's/.*id=([0-9]+).*/\1/'. Each exposes the capture group in a different way.
Why might a pattern that works in your JavaScript console give different output in grep -E?
Different dialects: ERE has no lookaround and no non-capturing groups in strict POSIX, picks the longest alternative rather than the first, and some tools lack \d or \b. Test the pattern in the tool you will actually run.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.