Google Sheets Functions for the Advanced Online Marketer – Part IV: REGEX(MATCH/EXTRACT/REPLACE)

Whether you regularly connect various spreadsheets to create impressive reports or only use Google Sheets once a month to update numbers, every online marketer should have the following Google Sheets functions in their toolbox.

These functions are not only incredibly useful for efficiently performing calculations in your spreadsheets, but they also open up new possibilities, allowing you to automate more tasks and achieve even better results.

Today Part IV: REGEXMATCH/ REGEXEXTRACT/ REGEXREPLACE. With these functions, you can apply the wonderful world of regular expressions (regex) to your spreadsheets, making it incredibly easy to compare, extract, or replace things.

REGEX functions

What are REGEX functions used for?

The three REGEX functions in Google Sheets can be used for various purposes. To give you some examples:

You can use REGEXMATCH to compare a piece of text with something. For example, if you want to check in an IF function whether cell A2 contains the value "test" somewhere (e.g., in a URL):

=IF(REGEXMATCH(A2;”test”);”delete”;”keep”)

This is a simple example that you could also do with a MATCH function, but as soon as a more complex comparison is involved, you can't avoid using REGEXMATCH.

For example, if you want to test a list of email addresses for invalid ones, you can use the following REGEXMATCH formula to perform the test:

=IF(REGEXMATCH(A2, “^[\w\.\-]+@[a-zA-Z\d\.\-]+\.[a-zA-Z]{2,}$”), “Valid”, “Invalid”)

You use REGEXEXTRACT to extract a part from a string. What you would normally accomplish with the LEFT, RIGHT, MID functions is often much simpler to do with a REGEXEXTRACT. And because of the capabilities of regex, you can take it much further. A simple example is extracting a domain from a URL:

=REGEXEXTRACT(A2;”\/\/([\w\.\-]+)\/”)

“\/\/([\w.-]+)\/” may seem like gibberish, but the principle is simple: look for a part of cell A2 that starts with // (after https://), followed by a combination of one or more (+) letters, numbers, or underscores (\w), periods \., or hyphens \-. By enclosing only the part we need with parentheses (), the result is as follows:

You can use REGEXREPLACE just like SUBSTITUTE to replace parts of a cell. For example, if you want to convert hyphens in a URL to spaces, you can use the following:

=SUBSTITUTE(A2;”-“;” “)

But also:

=REGEXREPLACE(A2,”\-“,” “)

So far, not much difference, but if you also want to replace underscores with spaces, the advantage of REGEXREPLACE becomes clear:

=SUBSTITUTE(SUBSTITUTE(A2,”-“,” “),”_”,” “)

would then be:

=REGEXREPLACE(A2,”(\-|_)+”,” “)

By placing the hyphens and underscores inside parentheses with a pipe | as a separator, you can replace multiple characters at the same time. By adding the +, you can also ensure that multiple hyphens or combinations of hyphens and underscores are replaced by just one space.

REGEXMATCH / REGEXREPLACE / REGEXEXTRACT bonus tip 1:

For some reason, Google Sheets does not allow the use of a field with only numbers in the REGEX functions, and you will receive the message: "Function REGEXMATCH parameter 1 expects text values. But '12345' is a number and cannot be coerced to a text."

However, you can easily solve this by combining the function with TO_TEXT to have the number interpreted as a text string:

=REGEXMATCH(TO_TEXT(A2),”\d+”)

REGEXMATCH / REGEXREPLACE / REGEXEXTRACT bonus tip 2:

All REGEX functions can also be used in combination with ARRAYFORMULA. This makes it even easier to check, replace, or extract things in bulk with these functions.

REGEXMATCH / REGEXREPLACE / REGEXEXTRACT bonus tip 3:

To get a good understanding of the possibilities of regex, it is recommended to take a regex course. Regexone.com offers an easy way to learn the basics of regex (for free), and Regexr.com makes it easy to test your regex on a variety of examples, so you can be sure it matches what it should match.

And the more you use regex functions, the more often you'll find other useful applications for them. For example, Channable, Google Analytics, Funnel.io, and many other tools also provide the option to apply regex.