Spreadsheet Practice

How to check if text matches a pattern in Excel

Excel formula

=REGEXTEST(A2,"^[A-Z]{3}-\d{4}$")

Use REGEXTEST in Excel 365: =REGEXTEST(A2,"^[A-Z]{3}-\d{4}$") checks for three capital letters, a dash and four digits. Google Sheets uses REGEXMATCH.

Result on the example
one result per row in B2, filled down to B7
Works in
Microsoft 365 only
Google Sheets
Different formula, see below

See it work

The formula is already in B2, filled down to B7. Change any input and the result updates, or edit the formula to experiment.

How it works

[A-Z]{3} means exactly three capital letters, - a dash, \d{4} exactly four digits, and ^ and $ pin the pattern to the start and end, so nothing extra is allowed. REGEXTEST is case-sensitive by default, so "nwd-1042" fails.

=REGEXTEST(A2,"^[A-Z]{3}-\d{4}$")

ResultTRUE

  1. A2"NWD-1042"

    The value in A2.

  2. REGEXTEST(A2,"^[A-Z]{3}-\d{4}$")TRUE

    REGEXTEST: TRUE if the text contains a match for a regular expression.

    • textA2"NWD-1042"
    • pattern"^[A-Z]{3}-\d{4}$"

In Google Sheets

Google Sheets needs a different formula: =REGEXMATCH(A2,"^[A-Z]{3}-\d{4}$"). On the same data it gives the same result. Here it is running in Google Sheets mode.

Other ways to do it

Each one was run on the same example sheet.

FormulaResultWhen to use it
=AND(LEN(A2)=8,MID(A2,4,1)="-",ISNUMBER(--RIGHT(A2,4)),EXACT(LEFT(A2,3),UPPER(LEFT(A2,3))))TRUE, FALSE, FALSE…Works in every version: checks the length, the dash, the digits and the capitals one by one. It doesn't check that the first three are letters.

Common mistakes

  • Without ^ and $, "XNWD-10420" passes because the pattern is found inside it.
  • REGEXTEST is only in Microsoft 365. Older versions show #NAME?; use the AND version there.

Try it yourself

Same data, empty answer cell. Your formula is graded instantly, then re-checked on changed data, so a typed-in number won't pass.

Check if text matches a pattern

In B2, return TRUE when the SKU is exactly three capital letters, a dash and four digits. Fill down to B7.

Questions

How do I make the match ignore case?

Set the third argument to 1: =REGEXTEST(A2,"^[A-Z]{3}-\d{4}$",1). In Google Sheets, start the pattern with (?i).

Can I use this to block bad entries?

Yes. Use it as a custom Data Validation formula for the column.

Checked by running on 2026-10-30: every formula and result on this page was calculated by the Spreadsheet Practice formula engine, in both Excel and Google Sheets modes.