How to Use the REGEXMATCH Function in Google Sheets

Published on

We can use the REGEXMATCH function for complex substring matching in Google Sheets, as it utilizes regular expressions for matching.

REGEXMATCH returns Boolean values (TRUE or FALSE), which can be used as conditions in other functions. Understanding this function is important as it can be combined or nested with functions like FILTER, COUNTIFS, SUMIFS, etc.

Two other functions use regular expressions: REGEXEXTRACT and REGEXREPLACE. Their names suggest their purposes.

Syntax:

REGEXMATCH(text, regular_expression)
  • text – The text to be tested against the regular expression pattern.
  • regular_expression – The regular expression pattern used to test the text.

Formula Examples: REGEXMATCH Most Common Use Cases

Here are some common use cases. Once you’ve learned them, you can advance further by referring to the RE2 regular expression syntax reference on GitHub. Google Sheets supports RE2 syntax, excluding Unicode character class matching.

Also, note that all examples are case-sensitive. After the examples, I’ll share how to make them case-insensitive.

Example 1: Match Substrings within a String

Enter the following string in cell A1:

He visited the site last Friday and Saturday

How do you find out whether this string contains the substring “Friday” or not?

You can use the following REGEXMATCH formula in cell B1:

=REGEXMATCH(A1, "Friday")

This will return TRUE since the cell contains “Friday”.

When you want to match multiple substrings within a string, you can separate each substring with a pipe in the regular expression:

=REGEXMATCH(A1, "Friday|Sunday")

If either substring is present, the formula will return TRUE.

If you replace the string in cell A1 with the following one, the formula will still return TRUE:

He goes hiking in the mountains on Sundays

Because the substring “Sunday” partially matches.

What if you want to exactly match the substring within a string using the REGEXMATCH function in Google Sheets?

Formula:

=REGEXMATCH(A1, "\b(Friday|Sunday)\b") // returns FALSE

Where \b asserts the position at a word boundary.

REGEXMATCH function to match substrings within a string in Google Sheets

Example 2: Match Content within Two Specific Characters Such as Brackets

This is another common use case of the REGEXMATCH function in Google Sheets.

For example, you might want to match specific text within an array such as {500, 400, 4001, 250, 300}. How do you do that?

=REGEXMATCH(A1, "\{.*400\,.*\}")

It will match the substring 400, and return TRUE since it is present in the text.

In this formula, you can replace {} with (), [], or <> to match a specific substring within them.

Explanation:

  • \{ matches the character {.
  • .* matches any character (.) any number of times (*).
  • 400\, matches the characters 400,.
  • \} matches the character }.

Example 3: REGEXMATCH for Exact Match of Multiple Strings

This is another common use case of the REGEXMATCH function, especially when used with functions such as FILTER, SUMIFS, COUNTIFS, etc.

If cell A1 contains the string “pineapple,” the following formula will return TRUE as it partially matches “apple” in “pineapple”:

=REGEXMATCH(A1, "apple") // returns TRUE

How do you match them exactly?

=REGEXMATCH(A1, "^apple$") // returns FALSE

Where ^ asserts the position at the start of a line and $ at the end of the line.

When you want to match multiple strings exactly, specify them separated by a pipe as follows:

=REGEXMATCH(A1, "^apple$|^orange$|^mango$")

When you want to REGEXMATCH values in a range, use the ARRAYFORMULA function. In the following example, the formula uses the above regular expression to test the range A1:A5:

=ArrayFormula(REGEXMATCH(A1:A5, "^apple$|^orange$|^mango$"))
REGEXMATCH and exact match of multiple strings

Other Common Patterns in Use

Here are some other common regular expression patterns that you may encounter in Google Sheets formulas:

  • (\d): matches digits
  • [0-9]: matches digits
  • [a-z]: matches the character range a-z, inclusive
  • [A-Z]: matches the character range A-Z, inclusive

For case-insensitive matches, specify (?i) in front of the patterns. For example, use the following formula to match multiple values in a range case-insensitively:

=ArrayFormula(REGEXMATCH(A1:A5, "(?i)^apple$|^orange$|^mango$"))

As a side note, I am skipping email address validation since there is a dedicated function called ISEMAIL in Google Sheets.

An Example of the REGEXMATCH Function in Real-Life Use in Google Sheets

When you have a list of household items in column A and their quantities in column B, you can use the following SUMIF formula to find the total quantities of “Sofa” and “Dining Table”:

=ArrayFormula(SUMIF(REGEXMATCH(A2:A, "Sofa|Dining Table"), TRUE, B2:B))

It follows the syntax: SUMIF(range, criterion, [sum_range])

Where:

  • range: ArrayFormula(REGEXMATCH(A2:A, "Sofa|Dining Table"))
  • criterion: TRUE
  • sum_range: B2:B

Resources

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Sort Data but Keep Blank Rows in Excel and Google Sheets

Using a dynamic array formula or the FILTER command, we can sort data and...

Lookup Values Under Categories in Excel and Google Sheets

We can use a combination of XLOOKUP and VLOOKUP in both Excel and Google...

Extract Items Under a Category from the Same Column in Excel

In Excel, you can use a combination of the XLOOKUP and DROP functions to...

How to Create a Searchable Table in Excel Using the FILTER Function

Finding specific records, or rows containing the required information, is straightforward in Excel using...

More like this

XMATCH Row by Row: Finding Values Across a Range in Google Sheets

Using the BYROW function with XMATCH in Google Sheets allows us to match values...

Limit Formula Expansion to a Specific Row in Google Sheets

In this tutorial, I’ll explain how to limit the expansion of an array formula...

3-D Referencing Structured Data Tables in Google Sheets

When you have several tables within a single sheet—not across multiple sheets in a...

8 COMMENTS

  1. Good day to you.

    I am using the following formula:

    =ARRAYFORMULA(IFERROR(REGEXMATCH(FLATTEN(AT7:INDIRECT(AT1&AT2)),JOIN("|",AV7:INDIRECT(AV1&AV2))),""))

    The challenge:

    “T” should generate True
    “TB” should generate False
    However, “TB” is currently generating True.

    Is it possible to modify the formula to ensure an exact match?

    Thanks.

    • I recommend using TEXTJOIN instead of JOIN to avoid errors associated with empty cells.

      Here’s my suggested formula, where I replaced AV with A and AT with B:

      =ARRAYFORMULA(IFERROR(REGEXMATCH(FLATTEN(A7:INDIRECT(A1&A2)), "^"&TEXTJOIN("$|^", TRUE, B7:INDIRECT(B1&B2))&"$"),""))

  2. I have tried the examples here in relation to a workbook I am using and they all return parse errors.

    Any suggestions as to where to check appreciated.

  3. I want to match inside query the expression: 6.1)

    How can I do that, I’ve tried:

    matches '6.1)' – not working

    matches '\6.1\)' – not working

    matches '\6.1)\)' – not working

    Please help

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.