Regex Flavors: Why a Pattern Works in One Editor and Not Another
A pattern that matches in one editor and finds nothing in another is almost never a typo. The two tools follow different regex flavors: same core ideas, different rules at the edges. Here is where those edges are, so you can tell a flavor problem from a mistake in your pattern.
Where regex flavors come from
There is no single regex language. Early Unix forms were standardized in POSIX.2 in 1992, while more complex syntax arose in Perl. Philip Hazel started PCRE in 1997, and it is used by tools such as PHP and Apache HTTP Server. Emacs has its own syntax that is incompatible with the others.
Newer engines borrow from these lineages. The ICU documentation says its patterns and behavior are based on Perl's regular expressions, and that it conforms to level 1 of Unicode Technical Standard #18. Borrowing isn't copying, though. Features such as Unicode tokens, lookaround, and character class subtraction only work if your flavor supports them.
For a real-world case, see how syntax highlighting uses regex: patterns written for one specific engine.
Escaping and flags
The same pattern can need different characters depending on where you type it.
A literal is not a string
In JavaScript, a regex can be a literal or built from a string. MDN notes that these two are the same expression, and that forward slashes inside a literal must be escaped:
/a\*b/
new RegExp("a\\*b")The string form needs the backslash doubled, because the string parser reads it first. Paste the second form into a find box and it will not match what you expect. Paste the first form into a tool that wants plain pattern text and the slashes become part of the pattern.
Flags change what a pattern means
Flags are not universal either. JavaScript has d, g, i, m, s, u, v and y. According to MDN, u treats a pattern as a sequence of Unicode code points and v is an upgrade to u with more Unicode features, so /\p{L}*/u works as a Unicode letter match. Flags cannot be added or removed after the expression is created. ICU lists its own set: x for comments, s for dot-all, m for multiline, and w for Unicode word boundaries.
Anchors shift too. In ICU, \A does not match after a newline within the input, unlike ^. Check what your tool means by "start" before you rely on it.
Lookbehind and Unicode shorthand
Lookbehind has limits
Lookbehind looks simple: (?<=\$)\d+ matches digits that follow a dollar sign. But engines set their own limits on what may go inside. ICU supports (?<=...) and (?<!...), but the length of the possible match must not be unbounded, so no * or + operators. A pattern that's fine in one flavor can be rejected outright in another. When a pattern errors on a lookbehind, shorten or bound it, or check the tool's documentation.
What does \w really match?
Most people read \w as "letters, digits and underscore". In ICU it is wider: it covers alphabetic characters, marks, decimal numbers and connector punctuation, plus two joiner characters. That means accented and non-Latin letters match. A tool with a narrower definition will skip them, and a pattern that finds café in one place will stop at caf in another.
Case-insensitive matching differs as well. ICU notes that Java's default case-insensitive matching is ASCII-only, while ICU's works with all Unicode characters. If your notes contain more than English text, test with real examples.
Replacement syntax is its own language
The find half gets all the attention. The replace half has its own rules. In ICU, a group is referenced with $n or by name:
Find: (\d{4})-(\d{2})-(\d{2})
Replace: $3/$2/$1Only $ and \ need escaping in ICU replacement text, and an unescaped $ that is not followed by a group reference is an error. Other tools use \1 or a different escape. If you want a worked walkthrough of groups themselves, see regex capture groups. The same $1 idea also appears in how $1 works in snippets, where it means something different: a tabstop, not a group.
Test the pattern where it will run
When a pattern misbehaves, work through this order:
- Find out which engine the tool uses. The manual usually says.
- Check the flags: case, multiline, dot-all, Unicode.
- Check escaping: literal or string, doubled backslashes, slashes.
- Check the replacement text separately from the pattern.
- Test with a short sample that includes the awkward cases, such as accented letters.
For command-line searching, searching your notes with grep and ripgrep covers tools that have their own flavors too.
Bottom line
Regex is portable in spirit, not in detail. Keep a note next to each saved pattern that names the tool it was written for, and expect to adjust escaping, flags and replacement text when you move it.
How Carets fits in
A saved pattern is only useful if you can find it again, along with the note saying which tool it belongs to. Carets keeps Markdown, plain text and code in one editor, so a pattern sits in a fenced code block next to the notes about its flavor, with syntax highlighting making the pattern readable. Notes are organized into files, projects and tags, so a "regex" tag can gather every pattern in one place, and your files stay plain text, portable and future-proof. Carets is native to iPhone, iPad and Mac, not a web wrapper.
Get Carets on the App Store for iPhone, iPad and Mac.
Frequently Asked Questions
What is a regex flavor?
It is the set of syntax and behavior rules a particular regex engine follows. Engines share a core, but they differ on features such as lookbehind, Unicode handling, flags and replacement syntax.
Why does my pattern work in one editor and not another?
The two tools probably use different engines, or the same engine with different flags or escaping rules. Check the documented flavor first, then flags, then escaping.
Is there one standard regex syntax?
No. POSIX.2 standardized early Unix forms in 1992, but Perl-style syntax, PCRE, ICU and JavaScript each developed their own rules.
Why does $1 fail in my replacement text?
Replacement syntax is separate from pattern syntax. In ICU, an unescaped $ that is not followed by a group reference is an error, and other tools use different group markers such as \1.