Text function

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])
ArgumentWhat it means
textRequiredThe text or cell to test.
patternRequiredThe regular expression the text should match.
case_sensitivityOptional0 (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

B2fx=REGEXTEST(A2,"^[\w.+-]+@[\w-]+\.[\w.]+$")
AB
1Email
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

B2fx=REGEXTEST(A2,"^[A-Z]{3}-\d{4}$")
AB
1Code
2nyc-1042FALSE

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

B2fx=REGEXTEST(A2,"^[A-Z]{3}-\d{4}$",1)
AB
1Code
2nyc-1042TRUE

Result: TRUE

With case_sensitivity set to 1, [A-Z] also accepts lowercase letters.

4Count rows that contain a digit

C2fx=SUM(--REGEXTEST(A2:A4,"\d"))
ABC
1Reference
2Order seven2
3Order 7
4PO 2291

Result: 2

REGEXTEST returns {FALSE;TRUE;TRUE}; -- turns them into 0 and 1, and SUM adds them.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 17 KB

Common errors and how to fix them

ErrorWhy it happensFix
#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.

LanguageFunction name
RussianРЕГТЕСТ
SpanishREGEXPRUEBA

Recipes that use REGEXTEST