TExcelTAD
Home › Regular Expressions › Regex Flavors Compared

Regex Flavors Compared

There is no single "regex language". There are flavours — families of engines that share a core but disagree on details. When a pattern from Stack Overflow doesn't work in your tool, a flavour difference is the usual culprit.

The main families

Family Used by Character
POSIX BRE grep, sed (default) old; + ? | ( ) { } need backslashes to be special
POSIX ERE grep -E, awk, egrep + ? | ( ) special; no \d, no lazy, no lookaround; leftmost-longest matching
Perl / PCRE Perl, PHP, grep -P, R, nginx the feature-rich standard most others copy
ECMAScript JavaScript, TypeScript PCRE-like core, own flags (g, y, d, v), variable-width lookbehind
Python re Python PCRE-like, (?P<n>) names, fixed-width lookbehind
Java / .NET JVM languages, C#, F#, PowerShell PCRE-like, very complete, .NET has extras
RE2-style Go, Rust regex, RE2, ripgrep linear time; no backreferences or lookarounds

Feature matrix

Feature JS Python Java .NET PCRE Go / Rust POSIX ERE
\d \w \s ✓ ✓ ✓ ✓ ✓ ✓ ✗ (use [[:digit:]])
Lazy *? ✓ ✓ ✓ ✓ ✓ ✓ ✗
Named groups (?<n>) (?P<n>) (?<n>) (?<n>) both both ✗
Backreferences ✓ ✓ ✓ ✓ ✓ ✗ ✓*
Lookahead ✓ ✓ ✓ ✓ ✓ ✗ ✗
Lookbehind any width fixed bounded any width fixed alts ✗ ✗
Atomic (?>) / possessive ✗ 3.11+ ✓ atomic only ✓ ✗ ✗
\p{L} with u/v ✗ ✓ ✓ ✓ ✓ ✗
Inline flags (?i) partial† ✓ ✓ ✓ ✓ ✓ ✗
Free-spacing x ✗ ✓ ✓ ✓ ✓ Rust only ✗
\A \z ✗ \A \Z ✓ ✓ ✓ ✓ ✗
Recursion (?R) ✗ ✗ ✗ balancing groups ✓ ✗ ✗
Linear-time guarantee ✗ ✗ ✗ optional ✗ ✓ engine-dependent

* POSIX defines backreferences for BRE; many ERE tools support them as an extension. † Newer JavaScript engines support modifier groups like (?i:...) but not a global (?i) prefix.

Same idea, different spelling

The pattern "a word at the start of a line, case-insensitive" in several tools:

Tool Pattern
JavaScript /^\w+/gim
Python re.findall(r"(?im)^\w+", s)
grep -E grep -Eio '^[[:alnum:]_]+'
grep (BRE) grep -io '^[[:alnum:]_]\+' (GNU)
sed sed -E 's/^([[:alnum:]_]+).*/\1/'

Leftmost-first vs leftmost-longest

Backtracking engines (and RE2-style engines by default) report the first alternative that works. POSIX engines report the longest match starting at the leftmost position. The same pattern can therefore return different text:

regex · try it yourself
/to|tomato/g
tomato
2 matches
#1 "to" at index 0
#2 "to" at index 4

JavaScript tried to first, it worked, so the engine stopped there and never considered tomato. The search then continued after index 2 and found the second to at index 4. A POSIX engine (for example grep -Eo 'to|tomato') would instead report the whole word tomato as a single match, because it prefers the longest possible match at the leftmost position.

Bracket expressions in POSIX

POSIX tools use named classes inside brackets instead of shorthands:

POSIX Meaning Rough JS equivalent
[[:digit:]] digit \d
[[:alpha:]] letter [A-Za-z] (locale-dependent)
[[:alnum:]] letter or digit [A-Za-z0-9]
[[:space:]] whitespace \s
[[:upper:]] / [[:lower:]] case [A-Z] / [a-z]
[[:punct:]] punctuation \p{P} (approx.)

Python vs JavaScript, live

The same text, the same idea, two engines — note how Unicode handling differs:

python · try it yourself
import re

s = "Đà Lạt is 1500m high"
print(re.findall(r"\b\w+\b", s))
print(re.findall(r"(?<=\d)m\b", s))
['Đà', 'Lạt', 'is', '1500m', 'high']
['m']
js · try it yourself
const s = "Đà Lạt is 1500m high";
console.log(s.match(/\b\w+\b/g));
console.log(s.match(/[\p{L}\d]+/gu));
console.log(s.match(/(?<=\d)m\b/g));
[ 'L', 't', 'is', '1500m', 'high' ]
[ 'Đà', 'Lạt', 'is', '1500m', 'high' ]
[ 'm' ]

Porting checklist

When moving a pattern between flavours, check:

  1. Escaping of the host language (raw strings? doubled backslashes?).
  2. Named groups: (?P<n>) vs (?<n>), and replacement syntax.
  3. Unicode: does \w include ạ? Is \p{L} available?
  4. Lookbehind width limits — or no lookarounds at all.
  5. Anchors: $ before a trailing newline matches in Python/PCRE/Java, not in JavaScript.
  6. Flags: inline (?i) support, g vs findall.

Test yourself

Which flavour family has no \d shorthand?

  1. PCRE
  2. POSIX ERE
  3. JavaScript
  4. Python

What does a POSIX engine return for to|tomato on tomato?

  1. to
  2. tomato
  3. mato
  4. Nothing

Which named-group syntax works in Python's re in all Python 3 versions?

  1. (?<n>...)
  2. (?P<n>...)
  3. (?'n'...)
  4. {n}