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
| Tool | Default flavor | Notes |
|---|---|---|
grep, sed | POSIX BRE | grep -E / sed -E for ERE; grep -P for PCRE2 (GNU only) |
awk | POSIX ERE | gawk adds \y word boundary; no \d anywhere |
rg (ripgrep) | Rust regex, linear time | rg -P swaps in PCRE2 for lookarounds and backrefs |
Python re, JS, Java, .NET, Ruby, PHP | Perl-style backtracking | Close cousins with different edges |
Go regexp, RE2 | RE2 | No backreferences, no lookarounds |
nginx location ~ | PCRE | ~* for case-insensitive |
Prometheus =~ | RE2, fully anchored | job=~"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, readableDefault 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 winsconsole.log("abcd".match(/ab|abcd/)[0]); // "ab" — Perl-style: first alternative that works winsOrder 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")); // falsePython's version is re.escape(); it escapes everything non-alphanumeric, which is fine — over-escaping punctuation is harmless.
Field notes
- macOS ships BSD
sedandgrep:sed -i ''instead ofsed -i, nogrep -P.-Eand-i.bakwork on both (sed -ris 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.
Java Pulled Primitive Patterns After Five Previews. JDK 28 Won't Compile Them.
Primitive types in patterns, instanceof, and switch previewed in every JDK from 23 to 27. On November 9 the Amber team withdrew the feature, and it won't be in JDK 28 even with --enable-preview. Here's why Valhalla changed the plan, what replaces it, and how to rewrite code that used it.
.NET 11 Is Out. Union Types Got the Applause, but unsafe Is What Changed.
.NET 11 shipped on November 10, 2026, and C# 15 finally has union types, the feature people have asked for since pattern matching arrived.
PHP 8.6 Finally Turns On the Session Settings You Were Told to Set in 2006
Every PHP hardening guide for two decades has told you to flip the same three session settings. PHP 8.6, due November 19, 2026, flips them for you: strict mode on, HttpOnly on, SameSite set to Lax.
Rust 1.100: The Never Type Is Finally Stable, and Cargo Learned to Wait
Rust 1.100 stabilizes the never type after ten years and five failed attempts, along with the core Allocator API and a DropGuard helper. The change most teams will notice is in Cargo: a one-line config that makes it skip any crate version published in the last few days.