REGEXEXTRACT Returns #N/A in Google Sheets
REGEXEXTRACT returns #N/A when a valid pattern finds no match, and #REF! when the pattern is invalid. Read the message, then use IFNA or fix the regex.
本文目录
Google Sheets REGEXEXTRACT returns #N/A when a valid regular expression finds no match in the referenced cell. A different failure occurs when the regular expression itself is invalid: the cell shows #REF!, and the message says parameter 2 is not a valid regular expression.
These errors need different fixes. A missing match might deserve a blank or a short fallback. An invalid pattern has to be rewritten before you hide anything.
Consider this building-maintenance ticket log in A1:C10:
| Ticket | Floor | Notes |
|---|---|---|
| T-101 | 3 | East stair leak assigned WO-1842 |
| T-102 | 5 | Buzzing ballast reported |
| T-103 | Lobby | Badge reader stuck; WO-2075 opened |
| T-104 | 2 | Inspect ceiling stain |
| T-105 | 4 | WO-3196 awaiting replacement part |
| T-106 | 1 | Door closer needs adjustment |
| T-107 | 6 | Filter replacement under WO-4281 |
| T-108 | Lobby | Reception light flickering |
| T-109 | 3 | Follow up on WO-5630 tomorrow |
To extract the work-order code from C2, enter this formula in D2:
=REGEXEXTRACT(C2,"WO-[0-9]{4}")
It returns WO-1842. The pattern looks for the literal WO- followed by exactly four digits. That same formula will not produce a code for every row, because some notes contain no work-order reference.
A valid pattern returns #N/A when the cell has no match
The note in C3 is Buzzing ballast reported. It contains no work-order code.
=REGEXEXTRACT(C3,"WO-[0-9]{4}")
The result is #N/A. The message is: Function REGEXEXTRACT parameter 2 value "WO-[0-9]{4}" does not match text of Function REGEXEXTRACT parameter 1 value "Buzzing ballast reported".
The formula is not necessarily broken, and the note is not necessarily incorrect. The cell simply does not contain the token you’re asking REGEXEXTRACT to find.
Compare it with C4, which contains Badge reader stuck; WO-2075 opened. The same expression:
=REGEXEXTRACT(C4,"WO-[0-9]{4}")
returns WO-2075. The formula evaluates the same pattern on both cells. Only one cell supplies a match.
Use IFNA when a missing code is acceptable
If the ticket log allows notes without work-order codes, a column of #N/A values is harder to read. Wrap REGEXEXTRACT with IFNA:
=IFNA(REGEXEXTRACT(C3,"WO-[0-9]{4}"),"No work order")
For C3, this returns No work order. For C4, the equivalent formula returns WO-2075.
If you prefer an empty-looking result, use an empty string as the fallback:
=IFNA(REGEXEXTRACT(C3,"WO-[0-9]{4}"),"")
The cell looks blank. It is not a genuinely empty cell: ISBLANK on that result is FALSE, LEN is 0, and COUNTA still counts it as 1. That difference matters if another formula later counts the column.
I would use IFNA here rather than IFERROR. IFNA handles the expected no-match #N/A without automatically suppressing a different error. The distinction is covered in IFERROR vs IFNA.
Don’t add the wrapper until you’ve checked that the pattern works on a note containing a known code.
An invalid regular expression returns #REF!
A different problem occurs when REGEXEXTRACT cannot interpret the pattern itself. Suppose you copy a regular expression that contains a positive lookahead:
=REGEXEXTRACT(C2,"WO-[0-9]{4}(?= )")
The (?= ) portion is a positive lookahead. Google Sheets uses the RE2 regular-expression engine, which does not support lookahead assertions. C2 definitely contains WO-1842, so the problem is not missing data.
The cell shows #REF!, and the message is: Function REGEXEXTRACT parameter 2 value "WO-[0-9]{4}(?= )" is not a valid regular expression.
REGEXEXTRACT rejects the expression before it can extract the code.
Rewrite the pattern instead of hiding the error
For this ticket log, the lookahead is unnecessary. The work-order code already has a recognizable structure:
=REGEXEXTRACT(C2,"WO-[0-9]{4}")
The corrected formula returns WO-1842.
If you need to make sure the four digits are not part of a longer number, use a supported word boundary rather than a lookahead:
=REGEXEXTRACT(C2,"WO-[0-9]{4}\b")
This also extracts WO-1842 from C2. A word boundary is not identical to a lookahead for a space. It checks the transition between word and non-word characters, so use it only when that matches your data.
The important point is that an invalid parameter 2 requires a pattern change. Wrapping the lookahead formula with IFERROR conceals the problem: =IFERROR(REGEXEXTRACT(C2,"WO-[0-9]{4}(?= )"),"hidden") displayed the fallback text. The same formula wrapped in IFNA still showed #REF! and the invalid-expression message. That is a good reason to prefer IFNA for genuine no-matches: it will not swallow this #REF!.
For more about unsupported lookahead, lookbehind, and backreferences, see Extract Email Addresses with REGEXEXTRACT. That guide covers the relevant RE2 restrictions in more detail.
A pattern anchored to the whole cell misses an embedded code
A regular expression can be valid and still be too tight for the text you’re searching. This formula uses ^ 与 $ to require the entire contents of C2 to be a work-order code:
=REGEXEXTRACT(C2,"^WO-[0-9]{4}$")
C2 contains East stair leak assigned WO-1842, not just WO-1842. The result is #N/A, with the message: Function REGEXEXTRACT parameter 2 value "^WO-[0-9]{4}$" does not match text of Function REGEXEXTRACT parameter 1 value "East stair leak assigned WO-1842".
Remove the anchors:
=REGEXEXTRACT(C2,"WO-[0-9]{4}")
It returns WO-1842. The difference is precise. ^WO-[0-9]{4}$ requires the complete cell value to match the code format. WO-[0-9]{4} searches for that format anywhere inside the text.
Anchors are useful when you are validating a complete value. They are usually the wrong choice when you are extracting a code embedded in a longer maintenance note. For this log, keep the pattern unanchored because the Notes column contains free text.
REGEXMATCH returns FALSE where REGEXEXTRACT returns #N/A
If you’re unsure whether the pattern matches a particular cell, REGEXMATCH is a simple diagnostic. For the note without a work-order code in C3:
=REGEXMATCH(C3,"WO-[0-9]{4}")
The result is FALSE, whereas =REGEXEXTRACT(C3,"WO-[0-9]{4}") returns #N/A. For C2, REGEXMATCH with the same pattern returns TRUE, and REGEXEXTRACT returns WO-1842.
REGEXMATCH answers whether the pattern matches. REGEXEXTRACT returns the matched text. You do not need to replace REGEXEXTRACT with REGEXMATCH in the finished extraction column.
Fill the corrected formula down the ticket log
Once the pattern works on a known matching row and you’ve confirmed how a non-matching row behaves, place this formula in D2 and fill it through D10:
=IFNA(REGEXEXTRACT(C2,"WO-[0-9]{4}"),"No work order")
The resulting output is:
| Ticket | Extracted work order |
|---|---|
| T-101 | WO-1842 |
| T-102 | No work order |
| T-103 | WO-2075 |
| T-104 | No work order |
| T-105 | WO-3196 |
| T-106 | No work order |
| T-107 | WO-4281 |
| T-108 | No work order |
| T-109 | WO-5630 |
This is the version I would keep for the maintenance log. It extracts the token the column needs and gives a readable result when no code is present.
If your real work-order identifiers have a different format, adjust the expression to match that format rather than adding a broad pattern that captures unrelated numbers. WO-[0-9]{4} deliberately expects four digits. It does not define how to handle five-digit identifiers or codes with different prefixes.
If your task is instead to extract arbitrary numeric values from mixed text, Extract Numbers from Text covers that separate problem.
For a REGEXEXTRACT error, the useful first action remains the same: inspect the error before changing the formula. A valid pattern with no matching code is a data condition you can handle with IFNA. An invalid parameter 2 is a #REF! you need to correct by rewriting the pattern.