Exact Match Using Regular Expression in Google Sheets

This post explains how to exact match single as well as multiple strings using regular expression in Google Sheets.

Actually, I have already explained the same in some of my earlier tutorials. But the central topic of those tutorials was different.

Further, the exact match of multiple strings using regular expression (pattern match) can be part of my a few of the future tutorials.

So instead of writing the steps, again and again, I thought better to write a tutorial on regular expression for an exact match so that I can link it in relevant tutorials in the future. This tutorial is the outcome of that thought process.

There are two functions in Google Sheets for regular expression match. They are REGEXMATCH and QUERY.

The REGEXREPLACE and REGEXEXTRACT are the two other functions that use regular expressions. We do not require these two functions for exact math.

Two Functions that Supports Exact Match Using Regular Expression in Google Sheets

Actually, we can use the REGEXMATCH for an exact match and return TRUE or FALSE whereas the QUERY for exact match and filter a range.

Please do note that, similar to QUERY, using the FILTER function with REGEXMATCH, we can filter the exact match strings. But the aggregation capability of QUERY will still be missing.

Below is the sample array to test our formulas (exact match regular expression matches).

Sample Data Range: A1:B8

ProductDelivery
Product 1Jan
Product 2Feb
Product 6Jan
Product 11Mar
Product 22Feb
Products 1 & 5Feb
Products 2 & 6Mar

Regular Expression Exact Match in REGEXMATCH in Google Sheets

Single Criterion (Condition)

Assume I want to test whether cell A2 contains “Product 1”. Then I should use the REGEXMATCH formula as below.

=REGEXMATCH(A2,"^Product 1$")

This will return TRUE if cell A2 contains the string “Product 1”. This is an exact match using regular expression in Google Sheets. It will not match “Product” or “Product 11”.

In order to exactly match a single criterion in a whole column, use the ARRAYFORMULA function with the REGEXMATCH.

=ArrayFormula(REGEXMATCH(A2:A,"^Product 1$"))

Wait! Before using this formula you need to include a logical expression to limit the expanding of results to non-blank cells.

I am normally using an IF and LEN combination which tests whether the length of a cell is greater than or equal to 0. If the cell has content, the LEN will return the length of the characters else 0.

=ArrayFormula(if(len(A2:A)=0,,REGEXMATCH(A2:A,"^Product 1$")))
Regexmatch Exact Match - Single Condition

If you are not familiar with the IF and LEN combination, then here is one alternative formula.

=ArrayFormula(if(A2:A="",,REGEXMATCH(A2:A,"^Product 1$")))

What about Using the Criterion for an Exact Match as a Cell Reference?

Let’s insert the criterion is cell D1. Then in the formulas above, use the criterion as below.

=ArrayFormula(if(len(A2:A)=0,,REGEXMATCH(A2:A,"^"&D1&"$")))

Multiple Criterion (Conditions)

In order to exact match multiple conditions using a regular expression in Google Sheets, separate each criterion with a | symbol. The Caret and Dollar signs must be repeated for each criterion.

=ArrayFormula(if(len(A2:A)=0,,REGEXMATCH(A2:A,"^Product 1$|^Product 22$|^Product 2$")))

Criteria in D1:D3 (Fixed Range):

To refer to multiple criteria, we can write the criteria as below but not advised!

=ArrayFormula(if(len(A2:A)=0,,REGEXMATCH(A2:A,"^"&D1&"$|^"&D2&"$|^"&D3&"$")))
Regexmatch Exact Match - Multiple Conditions ArrayFormula

It will work but it is not flexible because the cell references are referenced individually. So, in the future, we may require to modify the formula to add more conditions. We can make it flexible as below.

Criteria in D1:D (Open Range):

="^"&textjoin("$|^",true,D1:D)&"$"

This will help you include more criteria in column D in the future.

So the formula will be;

=ArrayFormula(if(len(A2:A)=0,,REGEXMATCH(A2:A,"^"&textjoin("$|^",true,D1:D)&"$")))

Filter Exact Match Using FILTER and REGEXMATCH in Google Sheets

If you could understand the regular expression use for an exact match in Google Sheets, then the filtering part will be easy for you.

Syntax: FILTER(range, condition1)

You just want to use the REGEXMATCH formula as the FILTER ‘condition1’.

Example:

=Filter(A2:B,ArrayFormula(if(len(A2:A)=0,,REGEXMATCH(A2:A,"^"&textjoin("$|^",true,D1:D)&"$"))))

You can remove the Arrayformula( and one of the closing brackets from the end part as the FILTER formula in Google Sheets doesn’t require the ARRAYFORMULA function within.

Filter Exact Match Using Regular Expression in Google Sheets

Regular Expression Match in Query

If your purpose of the exact match using regular expression in Google Sheets is just to filter or filter and aggregate data, then QUERY is the better option.

In Query, we can use the MATCHES clause for regular expression match of single as well as multiple criteria.

You May Like: What is the Correct Clause Order in Google Sheets Query?

Single Criterion:

=query(A2:B,"Select A,B where A matches 'Product 1'")

When the criterion is in the cell D1 (please refer to Examples to the Use of Literals in Query in Google Sheets):

=query(A2:B,"Select A,B where A matches '"&D1&"'")

Here also for multiple criteria, we can use the | separator. But unlike REGEXMATCH, the ^ and $ sign around the criterion is not required.

Criteria Hardcoded into the Formula:

=query(A1:B,"Select A,B where A matches 'Product 1|Product 22|Product 2'")

Cel References:

=query(A1:B,"Select A,B where A matches '"&TEXTJOIN("|",true,D1:D)&"'")

For more details about QUERY regular expression match, please check this tutorial – Matches Regular Expression Match in Google Sheets Query.

If you don’t want an exact match, then you may opt for;

That’s all about the exact match using regular expression in Google Sheets. Enjoy!

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.

Finding Most Frequent Text in Excel with Dynamic Array Formulas

Looking to identify the most frequently occurring text in Excel? You can do this...

Hierarchical Numbering Sequences in Excel

Creating hierarchical numbering sequences in an Excel spreadsheet can significantly improve the way you...

How to Easily Repeat a Sequence of Numbers in Excel

Excel offers multiple ways to accomplish tasks, and the simplicity of each approach depends...

Create a Sequence of Dates at Every Nth Row in Excel (Dynamic Array)

Would you like to create a sequence of dates in every nth row in...

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...

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.