Flavors and the common core

Beginner · 7 min

Regex is one idea with many dialects. See which features every engine shares, where JavaScript, Python, PCRE, Java, Go and grep diverge, and how to probe yours.

Before thisWhat a regular expression isLiterals and escapingTesting patterns in regex101 and the console

CodeLucky · 7 min

Flavors and the common core

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodeLuckyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • There is no single regex language. Each engine (JavaScript, Python re, PCRE, Perl, Java, Go, POSIX grep) implements its own dialect, called a flavour.
  • The common core is literals, ., character classes, anchors, * + ? {n,m}, | and ( ). A pattern built only from these ports almost everywhere.
  • Lookaround, named groups, lazy quantifiers and backreferences are where flavours split. Go's regexp rejects lookahead, lookbehind and backreferences.
  • In POSIX basic syntax (plain grep) + and | and ( ) are literals until escaped. grep -E flips that.
  • To find your flavour, read the tool's docs and probe it with a one-line test such as x(?=y) or (?<n>a), then run your real test cases.

The same word, different languages

Suppose you copy a pattern from a Stack Overflow answer into your project and it throws an error, or worse, silently matches different text. The pattern did not change. The engine did.

Regex started as a notation, and each tool that implemented it made its own choices about what to add. Perl added many features in the 1980s. Libraries such as PCRE copied Perl’s. JavaScript, Python, Java, Go and grep each built their own engine with their own gaps. Each of these dialects is called a flavour. A flavour is a specific engine’s rules for what a pattern means.

Think of it as the way a recipe can say “bake until done” while ovens run at different temperatures. The words are shared, and the results are not guaranteed.

What is in the common core

I ran thirteen small probes through seven engines on this machine: Node 26 (ECMAScript), Python 3.9, Perl 5.34, pcre2grep 10.49 (PCRE2), Java 21, Go 1.27’s regexp package and macOS’s grep -E. Each cell is the text that the pattern matched, NO if it ran and found nothing, or ERR if the engine rejected the pattern.

                   JS      Py3.9   Perl    pcre2   grep -E Java21  Go
digit shorthand    42      42      42      42      42      42      42
plus is operator   abbb    abbb    abbb    abbb    abbb    abbb    abbb
bare alternation   dogs    dogs    dogs    dogs    dogs    dogs    dogs
lazy star          aXb     aXb     aXb     aXb     aXb     aXb     aXb
non-capturing (?:  abab    abab    abab    abab    abab    abab    abab
lookahead          x       x       x       x       ERR     x       ERR
lookbehind         42      42      42      42      ERR     42      ERR
named (?<n>a)      ab      ERR     ab      ab      ERR     ab      ab
named (?P<n>a)     ERR     ab      ab      ab      ERR     ERR     ab
backreference      aa      aa      aa      aa      aa      aa      ERR
word boundary      cat     cat     cat     cat     cat     cat     cat
POSIX [[:digit:]]  NO      NO      42      42      42      NO      42
interval a{2}      aa      aa      aa      aa      aa      aa      aa

The top rows agree: \d+, ab+, (cat|dog)s, the lazy a.*?b, (?:ab)+, \b and a{2}. That is the practical common core: literals, the dot, character classes, anchors, the quantifiers * + ? and {n,m}, alternation, and parentheses. Write patterns from those and they move between languages with little change.

One warning on the grep -E column. It looks as capable as the rest because macOS’s grep goes beyond the POSIX standard, accepting \d, lazy *? and (?:. I did not run GNU grep or any strict POSIX implementation, so I won’t claim that the same patterns work there.

Where flavours split

The lower rows are the split points.

  • Lookaround (x(?=y), (?<=\$)\d+). Five engines accept it. grep -E and Go reject it.
  • Named groups. (?<n>a) is JavaScript, Perl, PCRE2, Java and Go. Python 3.9 wants (?P<n>a), which JavaScript and Java reject. Perl, PCRE2 and Go accepted both.
  • Backreferences ((a)\1). Everything but Go accepted them.
  • POSIX classes such as [[:digit:]]. JavaScript, Python and Java found nothing: they read [[:digit:] as a bracket set of characters and then a stray ]. Perl, PCRE2, grep -E and Go treated it as a digit class.

Go rejecting lookahead and backreferences is deliberate. Go’s regexp package documents RE2 syntax, a design that guarantees matching time linear in the input, and it gets that by leaving out features that need backtracking. I did not run RE2 itself or the .NET engine. The linear-time engines lesson comes back to the trade-off.

The POSIX tools

The standard Unix tools have two syntaxes. In basic regular expressions (BRE, the default for plain grep), {, (, | and + are literal until you escape them. In extended syntax (ERE, selected with grep -E or sed -E), they are operators:

$ grep 'a{2}b' bre.txt          a{2}b
$ grep 'a\{2\}b' bre.txt        aab
$ grep -E 'a{2}b' bre.txt       aab
$ grep '(cat|dog)s' alt.txt     (no output, exit 1)
$ grep '\(cat\|dog\)s' alt.txt  dogs, cats
$ grep -E '(cat|dog)s' alt.txt  dogs, cats

The file bre.txt held aab and a{2}b. See searching with grep for the flags themselves.

Finding out which flavour you have

Three steps, in order. Read the documentation of the function you call, because JavaScript’s RegExp, Python’s re and Java’s Pattern each document their syntax. Print a version, such as node -v, python3 -V or perl -e 'print "$]\n"', because features arrive over time. Then probe: run x(?=y) to see whether lookahead exists, and (?<n>a) against (?P<n>a) to see how names are spelled.

Finally, run your own test cases from the previous lesson in the engine you will ship on. Passing in a tester set to a different flavour counts for little.

About the video

CodeLucky’s “Regex Demystified: Cross-Language Regular Expression Differences for Beginners” was published on 8 September 2025 and runs 6 minutes 35 seconds. The chapters are Basic Syntax Variations, Delimiter Differences, Flag Variations Across Languages, Escape Character Differences, Character Class Differences, Language-Specific Features and Best Practices Summary. The description names Python, JavaScript, Java and Perl. I couldn’t read a transcript, so I haven’t checked a single claim it makes against what I ran above. It is also the weakest pick in this group: it compares programming languages, so the POSIX syntaxes, PCRE and Go in this lesson are not in it, and it has a very small audience. The San Diego Supercomputer Center webinar in the “deeper” list is the better source for grep’s three flavours, at 54 minutes with no chapters. English per titles and descriptions.

What I did not run

GNU grep, strict POSIX engines, RE2 itself, .NET, PHP and Ruby’s engine. The macOS grep -E column should not be read as POSIX behaviour.

Next

With the flavour question settled, the next group builds patterns from the ground up, starting with the square brackets: Character classes.

Check yourself

  1. 01Which features can you rely on in almost every flavour?

    Literals, the dot, character classes, ^ and $, the quantifiers * + ? and {n,m}, alternation with | and parentheses for grouping. Anything beyond that, such as lookaround or named groups, needs checking.

  2. 02Why does (?<year>\d{4}) fail in Python 3.9 but work in JavaScript?

    Python spells named groups (?P<name>...), and 3.9 does not accept the (?<name>...) form that JavaScript and Perl use.

  3. 03What happens to x(?=y) in Go's regexp package?

    It fails to compile. Go's engine runs in linear time and leaves out lookahead, lookbehind and backreferences.

  4. 04You write (cat|dog)s for plain grep and nothing matches. Why?

    Basic regular expressions treat ( ) and | as literal characters. Use grep -E, or escape them as \(cat\|dog\)s.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark