RE2 Regular Expression Syntax: What It Supports, What It Leaves Out, and When to Use It
A practical guide to RE2 regex syntax: supported features, missing constructs like backreferences and lookaround, Unicode behavior, and porting patterns safely.
RE2 is a regular expression library originally developed at Google. Its syntax looks familiar to anyone who has used Perl, PCRE, Python or JavaScript patterns, but it deliberately leaves out features that cannot be matched in time proportional to the input. This guide covers what RE2 syntax accepts, what it rejects, how its behavior differs from backtracking engines, and when another approach is a better fit.
A note on sources: no tool-specific test results, benchmarks or version data were supplied for this article. The descriptions below reflect RE2's published syntax documentation and general, verifiable behavior of the engine. Where details depend on the version or the language binding you use, that is flagged rather than guessed.
Is RE2 the right choice for your use?
RE2 is a strong choice when patterns or inputs come from sources you do not fully control, such as user-supplied search filters, log processing, configuration rules or network-facing services. Its main benefit is predictability: matching runs in time linear in the size of the input, so a badly written or hostile pattern cannot trigger the catastrophic backtracking that can stall engines such as PCRE or many built-in language engines.
The decisive tradeoff is expressiveness. RE2 does not support backreferences, lookahead, lookbehind, atomic groups, possessive quantifiers, recursion or conditionals. If your pattern depends on any of these, RE2 will reject it at compile time instead of running it.
- Choose RE2 when untrusted patterns or input are possible and you need bounded matching time.
- Choose RE2 when your patterns use standard features: classes, alternation, quantifiers, anchors, groups and named captures.
- Choose a backtracking engine when you genuinely need backreferences or lookaround and you control both pattern and input.
- Skip regular expressions entirely and use plain string operations (contains, starts-with, split, exact comparison) when you are matching a fixed literal. This is simpler to read, easier to review and avoids escaping mistakes.
What RE2 syntax actually changes compared with other engines
RE2 does not transform your text; it changes how a pattern is interpreted and executed. Instead of trying alternatives one by one and backtracking on failure, RE2 compiles the pattern into an automaton and scans the input without revisiting earlier choices. Features that require remembering arbitrary earlier text or testing ahead and then rewinding do not fit that model, which is why they are excluded rather than emulated.
Core syntax RE2 supports
| Construct | RE2 syntax | Notes |
| Any character | . | Does not match newline unless the s flag is set |
| Character class | [abc] [^abc] [a-z] | Negation and ranges as in most engines |
| Perl classes | \d \s \w \D \S \W | ASCII-only in RE2, not Unicode-aware |
| POSIX classes | [[:alpha:]] [[:digit:]] | Used inside brackets |
| Unicode classes | \pL \p{Greek} \PN \P{Han} | Use these for non-ASCII letters and scripts |
| Quantifiers | * + ? {n} {n,} {n,m} | Counted repetition has an upper limit set by the library |
| Lazy quantifiers | *? +? ?? {n,m}? | Supported |
| Alternation | a|b | Leftmost-first preference, like Perl |
| Capturing group | (re) | Numbered from 1 |
| Non-capturing group | (?:re) | Supported |
| Named group | (?P<name>re) | Newer releases also accept (?<name>re); check your version |
| Anchors | ^ $ \A \z | \Z is not supported |
| Word boundary | \b \B | ASCII word boundary |
| Literal text | \Q...\E | Treats the enclosed text literally |
| Hex and code point escapes | \x7F \x{10FFFF} | Supported |
Flags
Flags can apply to the rest of the pattern with (?flags) or to a single group with (?flags:re). A flag can be cleared with a minus sign, as in (?i-s).
- i: case-insensitive matching
- m: multi-line mode, so ^ and $ match at line boundaries as well as text boundaries
- s: lets . match newline
- U: ungreedy mode, which swaps the meaning of x* and x*? and similar pairs
(?i)error: (?P<code>\d+)
(?m)^WARN .*$
(?s:BEGIN.*?END)
\Q1+1=2\E
Constructs RE2 rejects
| Construct | Typical syntax elsewhere | RE2 behavior |
| Backreference | \1 \k<name> | Not supported |
| Lookahead | (?=re) (?!re) | Not supported |
| Lookbehind | (?<=re) (?<!re) | Not supported |
| Atomic group | (?>re) | Not supported |
| Possessive quantifier | x*+ x++ | Not supported |
| Recursion and subroutines | (?R) (?1) | Not supported |
| Conditionals | (?(1)a|b) | Not supported |
| Match-reset and continuation | \K \G | Not supported |
| End before final newline | \Z | Not supported; use \z or an explicit optional newline |
| Grapheme cluster | \X | Not supported |
A common workaround for missing lookaround is to capture the part you need and ignore the rest. For example, instead of a lookbehind that matches a number only after a dollar sign, match the dollar sign and capture the number.
PCRE: (?<=\$)\d+
RE2: \$(\d+) then read capture group 1
Repeated-word detection is different: a pattern such as (\w+) \1 has no RE2 equivalent, because it needs to remember arbitrary earlier text. That check has to be done in program code after matching, or with a different engine.
Unicode, combining marks and emoji
RE2 works on UTF-8 text by default, with a Latin-1 mode available in some bindings. In UTF-8 mode, . matches a single Unicode code point, not a single user-perceived character. A letter followed by a combining accent is two code points, and many emoji, including flags, skin-tone variants and family sequences joined with zero-width joiners, are several code points. Because RE2 has no \X grapheme construct, a pattern like ^.$ will not match such a sequence, and a length limit such as .{1,10} counts code points rather than visible characters.
Also note that \w, \d, \s and \b are ASCII-only in RE2. A pattern like \w+ will stop at an accented letter or a non-Latin script. Use Unicode classes such as [\pL\pN_]+ when the text may contain non-ASCII words, and normalize text in your application if precomposed and decomposed accents must match the same way.
Match semantics
By default RE2 reports the same match a Perl-style engine would choose, using leftmost-first preference among alternatives. Some bindings also offer a leftmost-longest (POSIX-style) mode. The two can return different results for patterns like a|ab, so confirm which mode your library uses before relying on alternation order.
Will a pattern still work after you copy it somewhere else?
RE2 syntax is shared, with small variations, by several implementations, most notably Go's regexp package, which follows RE2 syntax, and RE2 bindings for languages such as Python and JavaScript runtimes. Many products that need safe pattern matching also use RE2 or an RE2-compatible engine internally. The exact feature set can differ between these implementations and between versions, so a pattern that works in one is not automatically valid in another; check the documentation for the specific destination.
- Online regex testers often default to PCRE or JavaScript flavors. A pattern that passes there may use lookaround or backreferences that RE2 rejects. Select an RE2 or Go flavor if one is offered, and treat a pass in another flavor as unverified.
- Patterns moving from RE2 to a backtracking engine usually keep working, but the safety guarantee does not move with them. The same pattern may run very slowly on adversarial input in the new engine.
- Shorthand classes such as \w and \b may be Unicode-aware in the destination engine but are ASCII-only in RE2, so the same pattern can match different text.
- Named group syntax varies. (?P<name>re) is the long-standing RE2 form; check whether your destination expects that or (?<name>re).
- Escaping rules depend on where the pattern is stored. In source code, use raw string literals where available (backtick strings in Go, r-prefixed strings in Python) so backslashes reach RE2 unchanged. In JSON or YAML configuration, each backslash usually has to be doubled.
Go: regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
Python: re2.compile(r"\d{4}-\d{2}-\d{2}")
JSON: "pattern": "\\d{4}-\\d{2}-\\d{2}"
Readability is the practical limit on reuse. Long RE2 patterns without comments are hard to review, and RE2 has no extended mode that ignores whitespace and allows inline comments in all bindings. When a pattern will be maintained by others, keep a plain-language description and a few example inputs alongside it, and prefer several simple patterns or ordinary string code over one dense expression.
Gaps to verify for your setup
- The exact upper bound on counted repetitions and any memory limits in your binding.
- Whether your version accepts (?<name>re) in addition to (?P<name>re).
- Whether your binding exposes leftmost-longest matching or Latin-1 mode.
- How your destination product handles invalid UTF-8 input.
- Performance on your own data; no benchmarks were supplied, and linear-time matching does not mean every pattern is fast.
Bottom line
RE2 syntax covers the everyday core of regular expressions and trades away backreferences, lookaround and related features for guaranteed linear-time matching. Use it when safety and predictable performance matter more than advanced constructs, rewrite lookaround as captures where possible, and use Unicode classes instead of ASCII shorthand for international text. For fixed strings, plain text comparison is simpler and clearer than any pattern.