REGEXMATCH Function in Google Sheets (Easy Examples)

REGEXMATCH function is one of the most underrated functions of Google Sheets. Most users often choose not to use it as they think it’s probably going to be too complex.

The presence of regular expressions in the formula often puts people off.

However, with a little guidance, you can begin to incorporate the function into your spreadsheets and reap its benefits.

In this tutorial, I will cover how to use the REGEXMATCH function in Google Sheets and show you some simple examples that will further help you understand how to use it in your day-to-day work.

What does the REGEXMATCH Function Do?

The REGEXMATCH function belongs to Google Sheets’ suite of REGEX functions along with functions like REGEXEXTRACT and REGEXREPLACE.

Its main task is to find if a string of text matches a regular expression.

The function returns a TRUE if the text matches the regular expression’s pattern and a FALSE if it doesn’t.

What is a Regular Expression (REGEX)?

A regular expression is a text pattern. It is a sequence of characters, including symbols, letters, and numbers that are combined to form a search string.

A regular expression is often characterized by certain special symbols, also known as ‘metacharacters’.

The metacharacters are meant to represent either a single character, a sequence of characters, or one of the characters in a set.

Here are some metacharacters that the REGEXMATCH function supports:

Metacharacter

What it Represents

^ The beginning of the string
$ The end of the string
. A single character
? Zero or one occurrence of a character or string
* Zero or more occurrences of a character or string
+ One or more occurrences of a character or string
| The Or operator
() This holds a group of metacharacters inside it and represents that sequence of characters
[] This holds a set of characters and represents any one of the characters inside it
[^] This holds a set of characters and represents any one of the characters not listed inside it
\ This is used to escape a special character

A regular expression containing a combination of these metacharacters, along with other alphanumeric and/or special characters can help you find specific characters, strings, or patterns in a text string.

For example, the regular expression ‘^gr(a|e)y$’ matches strings that start with ‘gr’ followed by either an ‘a’ or an ‘e’ and ends with a ‘y’.

Syntax of the REGEXMATCH Function

The syntax for the REGEXMATCH function is as follows:

REGEXMATCH(text, reg_exp)

Here,

  • text is the string or value to be tested for whether it matches the regular expression.
  • reg_exp is the regular expression the text is compared to.

Applications of the REGEXMATCH Function (Examples)

The REGEXMATCH function can be quite helpful when you want to search for the existence of a particular search string or pattern in the contents of a cell.

Here are some useful applications of the REGEXMATCH function:

  1. You can use it to identify cells in a range that contain a particular letter, word or phrase.
  2. You can use it to identify cells that start with or end with a particular character or group of characters.
  3. You can use it to find the exact match of a string.
  4. You can use it to go through a list of social media posts to identify posts that contain hashtags.
  5. You can use it to find or validate email addresses, credit card numbers, IDs, etc. or any other format

There are many other ways in which you can apply the REGEXMATCH function. Once you start getting comfortable with REGEXMATCH and regular expressions, you will find that the possibilities are endless.

Let us see how REGEXMATCH can be used in each of the above applications

Using the REGEXMATCH Function to Identify Cells that Contain a Letter, Word, or Phrase

Let’s first see how you can use REGEXMATCH to identify if a cell contains a specific letter, word, or phrase.

If you want to know if cell A1 contains the word “good” then use the REGEXMATCH function as follows:

=REGEXMATCH(A1,"good")

Check for a word using REGEXMATCH

If you want to know if cell A1 contains any of the words ‘good’ , ‘wonderful’ or ‘amazing’, then that’s easy too.

We know that the metacharacter ‘|’ represents an Or operation. So you need to use the REGEXMATCH with a regular expression as shown below:

=REGEXMATCH(A1, "good|wonderful|amazing")

Check for multiple words

If you want to know if cell A1 contains numbers between 0-9, then you will need to use the square brackets as shown:

=REGEXMATCH(A1, "[0-9]")

In the below example, it returns FALSE and there’s no number in cell A1.

Check for a number in a cell 2

In the below example, it returns TRUE, as there’s a number in cell A1.

Check for a number in a cell

Using the REGEXMATCH Function to Identify Cells that Start or End with a Specific Text

If you want to know if cell A1 starts with the word ‘you’ or ‘You’, then you need to use the ‘^’ metacharacter( since it represents the beginning of a string) along with the ‘|’ metacharacter.

So the function you will need to use is:

=REGEXMATCH(A1, "^you|You")

Check if the word starts with

 

If you want to know if cell A1 ends with the word ‘you’ or ‘You’, then you need to use the ‘$’ metacharacter( since it represents the end of a string) along with the ‘|’ metacharacter.

So the function you will need to use is:

=REGEXMATCH(A1, "you|You$")

String End with a specific character

If you want to know if cell A1 starts with ‘You’ and ends with ‘good’ then you need to use a combination of both ‘^’ and ‘$’ metacharacters:

=REGEXMATCH(A1, "^You[a-zA-Z ]+good$")

Starts with and Ends with a specific word

 

Here, we added “[a-zA-Z ]+” in the middle to represent any number of alphabets, both in lower and upper case, in between the words ‘You’ and ‘great’.

Note that there is a space after the ‘Z’ too because we also want to allow spaces to be present in the sequence.

The ‘+’ sign represents any number of letters and/ or spaces.

Space in the REGEXMATCH formula

Using the REGEXMATCH Function to Find the Exact Match of a String

Let’s say you want to know if cell A1’s contents exactly match the string “you”

For this, all you need to do is use the REGEXMATCH function as follows:

=REGEXMATCH(A1,"^you$")

Find exact match of the string

We get a FALSE in the result shown above because cell A1 contains the word “you” but does not contain only the word “you”.

Using the REGEXMATCH Function to Identify Hashtags among Social Media Posts

Let us say you have a set of social media posts in different cells of a column and you want to identify only those posts with hashtags in them.

Data with hashtags

For this, you will need to find if each cell has the character ‘#’ in it. So, you can perform the following steps:

  1. Select the cell you want to get your TRUE/FALSE result in (B1 in our example)
  2. Type the formula: =REGEXMATCH(A1,”#”). Don’t forget to put the regular expression within double-quotes.
  3. Press the return key.
  4. You should now see a TRUE or FALSE value indicating if the string in A1 has a hashtag or not.Find text string with hashtag
  5. Double click on the fill handle of B1.
  6. You should now see the results for all the cells in the column.Apply the formula to the entire column

Using the REGEXMATCH Function to Find Email Addresses in a Range

Let us see one final example of how powerful the REGEXMATCH function can be.

Email address dataset

Let us suppose you want to find all the cells in a range (shown in the image above) that contain email addresses.

For this, we first need to put together a regular expression for the pattern of an email address.

We know that:

  • Every email address starts with a string containing letters, numbers, dot operators. hyphens and/or underscores.
  • This should then be followed by an ‘@’ symbol.
  • This should be followed by another string containing letters and/ or numbers.
  • This should then be followed by a dot operator (.)
  • The email address should end with a string containing only letters.

For the above rules, we can formulate a regular expression as follows:

  • Every email address starts with a string containing letters, numbers, dot operators, hyphens, and/or underscores. So the regular expression can start with: ^[a-zA-Z0-9.-_]+.
  • This should then be followed by an ‘@’ symbol. So the expression can now be changed to ^[a-zA-Z0-9.-_]+@
  • This should be followed by another string containing letters and/ or numbers So the expression can now be changed to ^[a-zA-Z0-9.-_]+@[a-zA-Z0-9]+
  • This should then be followed by a dot operator (.). Since the dot operator might get mistaken for the dot metacharacter, we use a ‘\’ metacharacter to escape it. So the expression can now be changed to: ^[a-zA-Z0-9.-_]+@[a-zA-Z0-9]+\.
  • The email address should end with a string containing only letters. So the expression can now be changed to: ^[a-zA-Z0-9.-_]+@[a-zA-Z0-9]+\.[a-zA-Z]+$
Note: a-z represents any lowercase character, while A-Z represents any uppercase character. 0-9 represents any numeric character between 0 and 9. The plus metacharacter (+) specifies that there can be one or more occurrences of the characters mentioned within the square brackets [].

So our final regular expression can be:

^[a-zA-Z0-9.-_]+@[a-zA-Z0-9]+\.[a-zA-Z]+$

This is not perfect and completely foolproof, but to keep things simple, let’s go with it. It should work with our sample data.

Now let us put this to use in our sample dataset. Follow these steps:

  1. Select the cell you want to get your TRUE/FALSE result in (B2 in our example)
  2. Type the formula: =REGEXMATCH(A2,” ^[a-zA-Z0-9.-_]+@[a-zA-Z0-9]+\.[a-zA-Z]+$”). Don’t forget to put the regular expression within double quotes.
  3. Press the return key.
  4. You should now see a TRUE or FALSE value indicating if the string in A2 is an email address or not.REGEXMATCH formula to find email address
  5. Double click on the fill handle of B2.
  6. You should now see the results for all the cells in the column.Apply REGEXMATCH to the entire column

Now you can clearly see which of the cells contain email addresses.

Subsequently, you could use a filter to display only the cells that contain email addresses if required.

These were a few simple examples of how the REGEXMATCH function can save time and increase efficiency.

Points to Remember

Here are a few important points that you need to remember when using the REGEXMATCH function.

  • This function only works with text input. It does not work with numbers
  • If you want to use numbers as input (for example telephone numbers), you need to first convert it to text, using the TEXT function.
  • The REGEXMATCH function is case-sensitive. Therefore, you will need to specify the correct case inside the regular expression or convert the entire input string to upper or lower case using the UPPER or LOWER functions.

The REGEXMATCH function can have numerous applications, once you learn how to use it effectively.

Strong knowledge of regular expressions helps, and the best way to get a good hold over it is to practice.

Play around with different regular expressions and see what results you get.

You will be surprised by how useful the REGEXMATCH function can be once you start using it on your day-to-day spreadsheet data.

I hope you found this tutorial useful!

Other Google Sheets Tutorials you may find useful:

Leave a Comment