# How to use REGEXMATCH with IF in Google Sheets

REGEXMATCH returns TRUE or FALSE, so it slots straight into IF: =IF(REGEXMATCH(A2,"^\d{5}(-\d{4})?$"),"Valid","Check").

**Google Sheets:** `=IF(REGEXMATCH(A2,"^\d{5}(-\d{4})?$"),"Valid","Check")`

**Excel:** Excel for Microsoft 365 has REGEXTEST, which works the same way: `=IF(REGEXTEST(A2,"^\d{5}(-\d{4})?$"),"Valid","Check")`. In older versions, check the length and digits instead: `=IF(AND(LEN(A2)=5,ISNUMBER(--A2)),"Valid","Check")`, which accepts only the 5-digit form.

## Example

Sample data (row 1 is the header):

| A | B |
| --- | --- |
| ZIP code | Check |
| 55401 |  |
| 5540 |  |
| 55401-1234 |  |
| 554O1 |  |
| 10001 |  |
| ABCDE |  |
| 02134 |  |

Formula in B2, filled to B8: `=IF(REGEXMATCH(A2,"^\d{5}(-\d{4})?$"),"Valid","Check")`

Result: Valid; Check; Valid; Check; Valid; Check; Valid

## How it works

REGEXMATCH returns TRUE when the text matches the pattern. `^` and `$` pin the match to the start and end, `\d{5}` means exactly five digits and `(-\d{4})?` allows an optional dash and four more. Without the anchors, "123456" would pass because it contains five digits. "554O1" has a letter O and fails.

## Other ways

- `=IF(AND(LEN(A2)=5,ISNUMBER(--A2)),"Valid","Check")`: Without regular expressions: five characters that convert to a number. Works in both apps. (Rejects the ZIP+4 code 55401-1234.)
- `=IF(REGEXTEST(A2,"^\d{5}(-\d{4})?$"),"Valid","Check")`: The Excel for Microsoft 365 equivalent.

## Common mistakes

- REGEXMATCH needs text. A ZIP code stored as a number returns an error; wrap it: `REGEXMATCH(TO_TEXT(A2),…)`.
- Matching is case-sensitive. Start the pattern with `(?i)` to ignore case.
- ZIP codes stored as numbers lose their leading zero (02134 becomes 2134). Keep the column as plain text.

## Questions

**How do I check an email format?**

`=REGEXMATCH(A2,"^[^@\s]+@[^@\s]+\.[^@\s]+$")` checks for one @ with text on both sides and a dot after it.

**How do I pull out the matching part instead?**

Use REGEXEXTRACT: `=REGEXEXTRACT(A2,"\d{5}")`.

Every formula and result on this page was checked by running it in the Spreadsheet Practice formula engine on 2026-10-30.

Source: https://spreadsheetpractice.com/formulas/regexmatch-with-if
