REGEXTEST function
Checks whether text matches a regular expression pattern and returns TRUE or FALSE. Great for validating codes and emails.
Available in Microsoft 365 only (Windows, Mac and Excel for the web; rolled out from 2024)
Microsoft 365 onlyNot in Google Sheets
Syntax
REGEXTEST(text, pattern, [case_sensitivity])| Argument | What it means | |
|---|---|---|
text | Required | The text or cell to test. |
pattern | Required | The regular expression the text should match. |
case_sensitivity | Optional | 0 (default) case-sensitive; 1 ignore case. |
Returns: TRUE if any part of the text matches the pattern, otherwise FALSE.
How REGEXTEST works
REGEXTEST is a yes/no question: does this text contain something that fits the pattern? Use it to validate data (is this a properly formed product code, postcode or email?), to flag rows in IF formulas, or as the condition inside FILTER and conditional formatting.
A pattern matches if it is found anywhere in the text. To insist that the whole cell has a given shape, anchor it with ^ at the start and $ at the end: "\d{4}" is TRUE for Order 20456 (it contains four digits), but "^\d{4}$" is TRUE only for exactly four digits. Quick pattern guide: \d a digit, \w a letter, digit or underscore, \s whitespace, . any character, [A-Z] one capital letter, [^0-9] anything except a digit, + one or more, * zero or more, {4} exactly four, ^ start of text, $ end of text, ( ) a capture group, | or.
Matching is case-sensitive unless you pass 1 as the third argument. REGEXTEST works on arrays too, so =SUM(--REGEXTEST(A2:A100,pattern)) counts matching rows.
Examples
1Validate an email address
| A | B | |
|---|---|---|
| 1 | ||
| 2 | [email protected] | TRUE |
Result: TRUE
The pattern requires name characters, an @, a domain and a dot followed by more characters, from the start to the end of the cell.
2Strict product-code format
| A | B | |
|---|---|---|
| 1 | Code | |
| 2 | nyc-1042 | FALSE |
Result: FALSE
The format is three capitals, a hyphen and four digits. The code is in lowercase, and matching is case-sensitive by default, so the test fails.
3Same check ignoring case
| A | B | |
|---|---|---|
| 1 | Code | |
| 2 | nyc-1042 | TRUE |
Result: TRUE
With case_sensitivity set to 1, [A-Z] also accepts lowercase letters.
4Count rows that contain a digit
| A | B | C | |
|---|---|---|---|
| 1 | Reference | ||
| 2 | Order seven | 2 | |
| 3 | Order 7 | ||
| 4 | PO 2291 |
Result: 2
REGEXTEST returns {FALSE;TRUE;TRUE}; -- turns them into 0 and 1, and SUM adds them.
Common errors and how to fix them
| Error | Why it happens | Fix |
|---|---|---|
#VALUE! | The pattern is not a valid regular expression. | Check for unbalanced brackets and escape special characters. |
#NAME? | The function is not available in your version. | Use Microsoft 365, or approximate with ISNUMBER(SEARCH()) for simple checks. |
Tips
- Use REGEXTEST in Data Validation (Custom rule) to stop badly formed codes being typed in the first place:
=REGEXTEST(A2,"^[A-Z]{3}-\d{4}$"). - An empty cell returns FALSE for most patterns; combine with
A2=""if blanks are allowed. - Patterns are not like COUNTIF wildcards:
*means "repeat the previous item", not "anything". Use.*for anything.
Questions
Why does my pattern match more than I expected?
Without ^ and $ the pattern can match anywhere inside the text. Add them to require that the whole cell matches.
Can I use REGEXTEST in conditional formatting?
Yes. Use a formula rule such as =NOT(REGEXTEST($A2,"^\d{5}$")) to highlight postcodes that are not five digits.
Google Sheets
Use REGEXMATCH(text, regular_expression), which returns TRUE or FALSE (RE2 syntax).
REGEXTEST in other languages
In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.