ShareYourGroupShareYourGroupGoogle Sheets

Text

REGEXMATCH function in Google Sheets

REGEXMATCH tests whether a text matches a pattern and returns TRUE or FALSE.

A regular expression is a pattern that describes a shape of text: "starts with two capital letters", "contains a digit", "ends in .com". REGEXMATCH asks whether a cell fits the pattern. It turns checks that would need a tangle of LEFT, MID and LEN into one short formula.

Syntax

=REGEXMATCH(text, regular_expression)
ArgumentWhat it is
textThe text to test. It must be text, not a number.
regular_expressionThe pattern, in quotation marks.

Examples

Sample sheet: Text as people really type it: stray spaces, mixed capitals, codes with dashes.
ABCD
1Full nameEmailCodeAmount as text
2  asha   verma asha.verma@example.comPB-10-12341,250
3RAVI KUMARravi@example.orgDL-08-0042300
4meena gillmeena.gill@example.comMH-12-9876 45.50 
5Kabir Khankabir@example.netRJ-14-000712 kg

How it works

REGEXMATCH returns TRUE if the pattern is found anywhere in the text, and FALSE otherwise. To insist that the whole cell fits, anchor the pattern with ^ at the start and $ at the end.

Sheets uses the RE2 flavour of regular expressions.

The building blocks

PatternMatches
abcexactly those letters
.any single character
\da digit
\wa letter, digit or underscore
\sa space or tab
[abc]any one of a, b, c
[A-Z]any capital letter
[^0-9]any character that is not a digit
^the start of the text
$the end of the text
a|ba or b
( )a group

And how many times:

PatternMeans
*none or more
+one or more
?none or one
{4}exactly four
{2,5}two to five

A character with a special meaning, such as a dot, a plus or a bracket, needs a backslash in front to be taken literally: \.

Case

Patterns distinguish capital and small letters. Put (?i) at the very start to ignore case, as in the third example.

Where it earns its keep

In FILTER. =FILTER(A2:F, REGEXMATCH(D2:D, "(?i)book|pen")) shows rows whose product contains either word. FILTER has no "contains" of its own.

In IF. =IF(REGEXMATCH(A2, "^\d{6}$"), "OK", "Check PIN") flags PIN codes that are not six digits.

In data validation. Under Data, Data validation, choose "Custom formula is" and give a rule such as =REGEXMATCH(A2, "^[A-Z]{5}\d{4}[A-Z]$") to accept only a certain shape of code.

In conditional formatting, with the same kind of formula, to colour the cells that do or do not fit.

In COUNTIF-style counts. =SUM(ARRAYFORMULA(--REGEXMATCH(A2:A100, "pattern"))) counts the cells that match.

Only text

REGEXMATCH works on text. Given a number, it returns #VALUE!. Convert first: =REGEXMATCH(TO_TEXT(A2), "^\d{10}$"), or A2&"".

Keep patterns honest

A pattern can check the shape of a value, not whether it is real. A text can look like a phone number or an email address and belong to nobody. Use patterns to catch typing slips, not as proof.

Common mistakes

In Excel

Excel 2021 and earlier have no regular-expression functions. Recent Microsoft 365 has added REGEXTEST for the same job. The usual substitute in older Excel is ISNUMBER(SEARCH(...)) with wildcards.

Related functions

The results on this page were worked out by us from the sample sheet; they were not produced by a script running Google Sheets. If Sheets shows you something different, please tell us.

This is an independent help site. It is not connected with or approved by Google. Google Sheets is a trademark of Google LLC, named here only to say which program a formula is for. The people and figures in the sample sheets are made up.