Skip to content

Lesson 3 of 13 · 20m

Dialects: BRE, ERE, PCRE, JavaScript, and RE2

Why a pattern that works in regex101 silently matches nothing in sed — flavor differences, escaping layers, and leftmost-longest versus leftmost-first.

You test a pattern on regex101 in PCRE2 mode, paste it into a sed one-liner in a deploy script, and it silently matches nothing. No error, no warning — the config file just doesn't get patched, and you find out in production. There is no "regex language." There are a dozen dialects sharing a family resemblance, and two different match policies underneath them.

The flavor map

ToolDefault flavorNotes
grep, sedPOSIX BREgrep -E / sed -E for ERE; grep -P for PCRE2 (GNU only)
awkPOSIX EREgawk adds \y word boundary; no \d anywhere
rg (ripgrep)Rust regex, linear timerg -P swaps in PCRE2 for lookarounds and backrefs
Python re, JS, Java, .NET, Ruby, PHPPerl-style backtrackingClose cousins with different edges
Go regexp, RE2RE2No backreferences, no lookarounds
nginx location ~PCRE~* for case-insensitive
Prometheus =~RE2, fully anchoredjob=~"api" matches only the exact value api

BRE versus ERE

In Basic REs, ( ) { } are literal unless escaped, and + and ? aren't operators at all (GNU accepts \+ and \? as extensions). ERE flips that:

# Swap "555-0142" to "0142-555"
sed 's/\([0-9]\{3\}\)-\([0-9]\{4\}\)/\2-\1/' phones.txt   # BRE: backslash soup
sed -E 's/([0-9]{3})-([0-9]{4})/\2-\1/' phones.txt       # ERE: same thing, readable

Default to -E everywhere; it works on both GNU and BSD tools. The only time BRE is worth writing on purpose is when you're patching a script that already uses it — consistency inside one file beats purity across files.

There is no \d in POSIX

grep -E '\d+' app.log does not match digits. GNU grep treats \d as a literal d (newer versions at least warn stray \ before d). Use [0-9] or [[:digit:]], or grep -P if you genuinely need Perl syntax.

Leftmost-longest versus leftmost-first

Both families find the leftmost match. They disagree about which match at that position:

$ echo "abcd" | grep -oE 'ab|abcd'
abcd                     # POSIX: longest alternative wins
console.log("abcd".match(/ab|abcd/)[0]);  // "ab" — Perl-style: first alternative that works wins

Order alternations longest-first in Perl-style engines (abcd|ab), or better, make the alternatives disjoint so policy doesn't matter. The trap is a pattern that silently changes meaning when it crosses engines: x|xy is deterministic under leftmost-longest and order-sensitive under leftmost-first. Any pattern you'll paste between sed, grep, and a JS codebase should be written so both policies agree.

The escaping stack

A pattern usually passes through three parsers: the shell, the host language, the regex engine. Each one eats a layer of backslashes. Single-quote in the shell. Raw strings in Python (r"\d+"). In JSON or YAML, every backslash doubles: "pattern": "^\\d{3}-\\d{4}$". And never interpolate unescaped user input into a pattern:

const escapeRe = s => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const host = "api.sudosecurity.org";
console.log(new RegExp("^" + host + "$").test("apixsudosecurity.org"));           // true — the dot is a wildcard
console.log(new RegExp("^" + escapeRe(host) + "$").test("apixsudosecurity.org")); // false

Python's version is re.escape(); it escapes everything non-alphanumeric, which is fine — over-escaping punctuation is harmless.

Field notes

  • macOS ships BSD sed and grep: sed -i '' instead of sed -i, no grep -P. -E and -i.bak work on both (sed -r is also accepted on both — on BSD it is a documented alias for -E).
  • On regex101, set the flavor dropdown to your actual engine before trusting the green highlight.
  • Prometheus matchers, Grafana label filters, and many Go tools anchor implicitly. When you mean "contains," say .*api.*.
  • RegExp.escape() is Baseline now — Safari 18.2, Firefox 134, Chrome 136, Node 24. The helper above still earns its keep on older runtimes.

Keep reading

Related articles from the SudoSecurity blog.