REGEXEXTRACT in Google Sheets pulls out the part of a cell that matches a regular expression, using the RE2 pattern syntax. An AI-written pattern can be too greedy and swallow more than intended, or too strict and match nothing, and both failures look like ordinary results. Test the pattern on a matching cell, a non-matching cell, and a tricky cell before using it on a column.
What you need
- The AI-written REGEXEXTRACT formula and its pattern
- Three test cells: one that should match, one that should not, one edge case
- The exact text each test cell should return
Step 1: Test the pattern on a cell that should match
Run the formula on the matching test cell and check the extracted text character by character. If it returns more than the intended part, the pattern is too greedy.
Step 2: Test it on a cell that should not match
Run the formula on the non-matching test cell. It should return an error or an empty result; if it returns text, the pattern is too loose.
Step 3: Test the edge case and fix the pattern
Run the formula on the edge case, such as a code with an extra digit or a name with a hyphen. Adjust the pattern until all three tests pass, then fill it down.
Worked example: synthetic order codes
The table below is a synthetic example. It shows the expected result of checking an AI-written REGEXEXTRACT pattern; no formula was run.
| Check | Value | Result |
|---|---|---|
| Test cell 1 | Order ORD-1234 | Returns ORD-1234 |
| Test cell 2 | Order ORD-123 | Three digits, should fail |
| Pattern as written | ORD-[0-9]+ | Matches both cells |
| Corrected pattern | ORD-[0-9]{4} | Matches only four digits |
Expected result (UNTESTED). the pattern ORD-[0-9]+ matches both ORD-1234 and the three-digit ORD-123, so it is too loose; ORD-[0-9]{4} matches only the four-digit code and leaves the three-digit one unmatched. This check was not run live; the table shows the expected result for the synthetic example.
Limits
- REGEXEXTRACT uses the RE2 syntax and only accepts text input; a number must be converted with TEXT first.
- A pattern with no match returns an error, so wrap it in IFERROR when a blank is the right result.
- REGEXEXTRACT returns only the first match in a cell, not every match.
- A pattern that matches an empty string can return an empty result that looks like a failure.
