REGEXTEST
Short answer
Validate codes and formats in one test
REGEXTEST(text, pattern, [case_sensitivity])
- Category
- Text & Data Cleanup
- Excel version
- 365
- Also searched as
- regex in excel, validate account code format, check text pattern, does cell match pattern, regular expression test
What it returns
REGEXTEST checks whether a string matches a regular expression and returns TRUE or FALSE. Point it at one cell and you get one answer. Point it at a column and the answers spill down, one per row.
=REGEXTEST("4200", "^\d{4}$") returns TRUE: the string is exactly four digits. =REGEXTEST("42OO", "^\d{4}$") returns FALSE, because those last two characters are the letter O.
The pattern is the asset. It writes down, in one place, what a valid code looks like. Every row is tested against the same rule, and nothing about the rule hides in a chain of nested functions.
REGEXTEST is a Microsoft 365 function. It is not in Excel 2024 or any perpetual version.
Arguments that matter
| Argument | Required | What it does |
|---|---|---|
text | Yes | The string to test. A cell, or a column such as GL[Account] to test every row. |
pattern | Yes | The regular expression, as a text string. "^\d{4}$" is four digits and nothing else. |
case_sensitivity | No | 0 case-sensitive (default). 1 case-insensitive. |
The pattern carries all the logic, so a few pieces of syntax cover most finance work:
^and$anchor the start and end. Without them, the pattern matches anywhere inside the string.\dis a digit,[A-Z]a capital letter,{4}exactly four of the previous item,{2,3}two or three.|means or.^(4|5)\d{3}$is a four-digit code starting with 4 or 5.\.is a literal dot. An unescaped.matches any character.
Leave the anchors off and "\d{4}" passes 14200-X, because four digits appear somewhere in it. That is the most common way a validation check passes rows it should catch.
In a financial model
REGEXTEST is a control. It belongs in the check row and beside the data, not in the calculation block.
Every ERP extract carries some codes that are not what they should be: a typo, a code from a retired entity, a cost center with a letter where a digit belongs. Each one either drops out of the totals or lands on the wrong line. The SUMIFS grid looks healthy either way.
A REGEXTEST column next to the extract finds them. Count the FALSE results and put the count in the check row, per build a check row and make it loud. The check sits where every reviewer sees it first, as in the check row lives at the top.
Then a FILTER on the same test lists the offending rows. The count says how many. The list says which.
Worked example
A GL extract formatted as a Table named GL, with columns Entity, Account, CostCenter, Amount. The chart-of-accounts rules: accounts are four digits, starting 1 to 8. Cost centers are two capital letters, a hyphen, and three digits, such as FN-210.
The check, as a calculated column Valid:
=AND(REGEXTEST([@Account], "^[1-8]\d{3}$"), REGEXTEST([@CostCenter], "^[A-Z]{2}-\d{3}$"))
| Account | CostCenter | Valid |
|---|---|---|
| 4200 | SA-110 | TRUE |
| 9100 | OP-220 | FALSE |
| 6100 | op-220 | FALSE |
| 610 | FN-210 | FALSE |
The second row fails on the account rule, the third on lowercase letters, the fourth on a three-digit account. In the check row:
=COUNTIF(GL[Valid], FALSE)
Expected zero. And on an exceptions tab, the rows to fix:
=FILTER(GL, GL[Valid]=FALSE, "All codes valid")
Old way vs new way
Testing a cost center like FN-210 without regex:
=AND(LEN(B2)=6, MID(B2,3,1)="-", ISNUMBER(--RIGHT(B2,3)), EXACT(LEFT(B2,2), UPPER(LEFT(B2,2))))
With REGEXTEST:
=REGEXTEST(B2, "^[A-Z]{2}-\d{3}$")
The old version still passes F1-210, because UPPER leaves digits alone, and ISNUMBER(--RIGHT(...)) accepts things like 1.5. The pattern states the rule exactly and has fewer places to be wrong.
Where it breaks
Every row passes, including bad ones. The pattern has no anchors, so \d{4} finds four digits somewhere inside 14200-X. Fix: wrap the pattern in ^ and $ so it has to match the whole string.
Valid codes fail the test. The extract carries a trailing space, or a non-breaking space from a web export, and $ refuses to match past it. Fix: clean the column first with REGEXREPLACE, or allow trailing whitespace in the pattern with \s*$.
Lowercase codes pass when they should not, or fail when they should pass. case_sensitivity defaults to case-sensitive. Fix: leave it at 0 when case is part of the rule, set 1 when it is not.
REGEXTEST returns an error instead of TRUE or FALSE. The pattern is malformed: an unclosed bracket or parenthesis, or a stray quantifier. Fix: build the pattern up one piece at a time on a test cell, and keep it in a named input cell so every formula reads the same rule.
A dot matches anything. "^\d{3}.\d{2}$" passes 100x25. The dot is a wildcard in regex. Fix: escape it as \. to match a literal dot.
#NAME? when a colleague opens the file. REGEXTEST is Microsoft 365 only. Excel 2024, 2021 and earlier do not have it. Fix: if the check has to run everywhere, validate in Power Query at load, or fall back to the longer LEN and MID test.
Related functions
- REGEXEXTRACT. Same pattern language, returns the matching text instead of TRUE or FALSE. Use it once the test tells you the value is in there.
- REGEXREPLACE. Cleans the column before you test it, so stray spaces and symbols do not trigger false failures.
- FILTER. Turns the test into a live exceptions list.
- COUNTIFS. Counts the failures for the check row, and can split them by entity.
- TEXTBEFORE. When the code has a fixed delimiter and you need a segment, not a yes or no.
