Regex generator for Excel and Google Sheets

Describe what you need to match. You get a pattern targeted at RE2 — the engine behind Excel's REGEXEXTRACT and Google Sheets' REGEXMATCH — so it will not quietly rely on features neither supports.

How to get a better answer
  • Give two or three strings that should match, and one that should not.
  • Say whether you want to test, extract or replace — they are three different functions.
  • Excel and Sheets both use RE2: no lookbehind, no backreferences. Patterns copied from Python often fail here.

How to use regex in Excel and Google Sheets

  1. 1

    Describe the pattern with examples

    Give two or three strings that should match and, just as usefully, one that should not. Edge cases are where regex goes wrong, and naming one up front is worth a paragraph of description.

  2. 2

    Pick the right function

    REGEXMATCH answers yes or no, REGEXEXTRACT pulls a piece out, REGEXREPLACE rewrites. Google Sheets has had all three for years; Excel gained REGEXTEST, REGEXEXTRACT and REGEXREPLACE in Microsoft 365 in 2024.

  3. 3

    Mind the RE2 limits

    Both engines are RE2, which has no lookbehind and no backreferences. Patterns copied from a Python or JavaScript answer often use both and simply fail here. The generator targets RE2 specifically.

  4. 4

    Escape the quotes when you paste

    A pattern containing a double quote needs that quote doubled inside an Excel string literal. This is the most common reason a correct pattern returns an error in the cell.

RE2 is not the regex you know

Excel and Google Sheets both use RE2, Google's regular expression engine, and RE2 deliberately omits features that can make matching take exponential time. The two you will miss are lookbehind and backreferences.

That means (?<=INV-)\d+ does not work, and neither does (\w+)\s+\1. Both are common in answers written for Python, JavaScript or .NET, which is why a pattern copied from Stack Overflow so often fails in a cell without explaining why.

Lookahead does work, and capture groups work for extraction — REGEXEXTRACT returns the first capture group if there is one. Most lookbehind problems can be rewritten as a capture group, and the generator does that rewrite for you.

Which function for which job

REGEXMATCH returns TRUE or FALSE and is what you want inside IF, FILTER or conditional formatting. In Excel 365 the same job is REGEXTEST.

REGEXEXTRACT returns the matched text, or the first capture group if your pattern has one. That last detail catches people out: adding brackets for grouping changes what comes back.

REGEXREPLACE rewrites and is the cleanest way to strip characters — removing everything that is not a digit from a phone column is one short pattern rather than a stack of nested SUBSTITUTEs.

When not to use regex

If the delimiter is fixed and the position is predictable, TEXTBEFORE, TEXTAFTER and TEXTSPLIT are easier to read and easier for the next person to maintain. Regex earns its place when the structure varies.

For splitting a column on a single character, SPLIT in Sheets or TEXTSPLIT in Excel 365 beats a pattern every time. For trimming known prefixes, SUBSTITUTE is clearer.

The honest test: if you can describe the rule without the word "or", you probably do not need regex. The generator will tell you when a plain text function is the better answer.

RE2 character classes and quantifiers

SyntaxMatchesIn Excel & Sheets (RE2)
\dAny digitSupported
\wLetter, digit or underscoreSupported
\sAny whitespaceSupported
[A-Z]Character rangeSupported
[^abc]Anything except a, b or cSupported
+ * ?One or more / zero or more / optionalSupported
{2,4}Between two and four timesSupported
(…)Capture groupREGEXEXTRACT returns the first group
(?:…)Group without capturingUse when you only need grouping
a|bEitherSupported
^ $Start / end of stringSupported
(?i)Case-insensitive from here onSupported — RE2 inline flag
(?=…)LookaheadSupported
(?<=…)LookbehindNOT supported — RE2 omits it
\1BackreferenceNOT supported — RE2 omits it
\bWord boundarySupported

Regex generator FAQ

Does Excel support regex?
Microsoft 365 gained REGEXTEST, REGEXEXTRACT and REGEXREPLACE in 2024. Older Excel versions have no regex functions at all — there you need VBA with the VBScript RegExp object, or a text-function workaround.
Why does my lookbehind pattern fail?
Because RE2, the engine both Excel and Sheets use, does not implement lookbehind. Neither does it implement backreferences. Rewrite the lookbehind as a capture group and use REGEXEXTRACT, which returns the first group.
Is the pattern case sensitive?
By default yes. Both REGEXMATCH and REGEXEXTRACT take a case-sensitivity argument in recent versions, and the inline flag (?i) works in RE2 as well. Say in your description if you want it case-insensitive.
How do I put a quote mark in the pattern?
Double it. Inside an Excel or Sheets string literal, one double quote ends the string, so a pattern needing a literal " has to write "". This trips up more correct patterns than any actual regex mistake.
Can I use this for Power Query or VBA?
Say so in your description. Power Query's M language and VBA's VBScript RegExp both use different engines that do support lookbehind and backreferences, so the pattern you want there is not the same one.
Will it explain the pattern?
Yes. Every answer breaks the pattern into its parts, so you can adjust it yourself rather than coming back for a regeneration when the data shifts.