C.W.K.
Stream
Lesson 08 of 10 · published

Flavor Differences — Where Lookaround Breaks

~8 min · flavors, compatibility

Level 0Pattern-Curious
0 XP0/90 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

Lookaround is the most fragmented feature in regex

You'd think "look around the position" would be universal. It isn't. Here's the actual landscape as of 2026:

EngineLookaheadLookbehindVariable-width Lookbehind
Python re✗ (raises error)
Python regex
JavaScript✓ (ES2018)✓ (ES2018)
PCREPartial (depends on version)
JavaLimited (max length must be bounded)
.NET
Go regexp / RE2
ripgrep (default)
ripgrep --pcre2Partial

The portability spectrum

Universal: No lookaround. Use anchors and character classes only.

Most engines: Lookahead (positive and negative). Almost always supported.

Most engines, but quirky: Lookbehind, fixed-width. Python, Java, .NET. Watch the width rule.

Modern engines only: Variable-width lookbehind. JavaScript ES2018+, .NET, Python regex.

Never: Go, RE2. Patterns using lookaround simply won't compile.

Practical advice

If your code needs to run in multiple languages or your regex might end up in a Go service, avoid lookaround. Use anchors, captures, and post-processing. The pattern is uglier but portable.

If you're in one engine and stay there, use lookaround freely — it's the cleanest tool for many problems.

Code

Same problem, different engines·text
# Goal: extract numbers preceded by '$' but skip the '$' itself

# Python re — works
import re
re.findall(r'(?<=\$)\d+', 'paid $19 not 20 but $5')
# ['19', '5']

# JavaScript ES2018+ — works
// 'paid $19 not 20 but $5'.match(/(?<=\$)\d+/g)
// ['19', '5']

# Go — does NOT work, regexp.MustCompile panics
// regexp.MustCompile(`(?<=\$)\d+`)  // panic: error parsing regexp

# Workaround for Go — capture and post-process
// re := regexp.MustCompile(`\$(\d+)`)
// matches := re.FindAllStringSubmatch(s, -1)
// for _, m := range matches { fmt.Println(m[1]) }  // m[1] is just the number

External links

Exercise

Take a lookaround pattern you've written. Test it in three engines via regex101: PCRE2, Python, ECMAScript (JavaScript). Note which work identically, which need flag changes, which fail. Pick a strategy for each: rewrite to avoid, or document the engine requirement.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.