4/24/2025

how to use regular expressions in google sheets

IN SUMMARY

Regular expressions (regex) are powerful tools for pattern matching and text manipulation in Google Sheets. They allow you to search, extract, and replace text based on specific patterns, making it easier to work with large datasets.

Basic Regex Syntax

Regular expressions match literal characters by default. For example, the regex 'hello' will match the string 'hello' in your data.

Certain characters have special meanings in regex, such as '.' (matches any character except newline), '\d' (matches any digit), '\w' (matches any word character), and more. These allow you to create more complex patterns.

Quantifiers specify how many times a pattern should be matched. For example, '+' matches one or more occurrences, '*' matches zero or more occurrences, and '?' matches zero or one occurrence.

Using Regex in Google Sheets

The REGEXMATCH function checks if a string matches a given regular expression pattern. It returns TRUE or FALSE. For example, '=REGEXMATCH(A2, "^[a-z]+$")' checks if the value in cell A2 contains only lowercase letters.

The REGEXEXTRACT function extracts a substring from a string based on a regular expression pattern. For example, '=REGEXEXTRACT(A2, "\d+")' extracts the first sequence of digits from the value in cell A2.

The REGEXREPLACE function replaces parts of a string that match a regular expression pattern with a new string. For example, '=REGEXREPLACE(A2, "\d+", "[REDACTED]")' replaces all digit sequences in the value of cell A2 with '[REDACTED]'.

Advanced Regex Techniques

You can group parts of a regex pattern using parentheses and reference them later using backreferences (\1, \2, etc.). This is useful for complex string manipulations.

Lookahead (?=pattern) and lookbehind (?<=pattern) assertions allow you to match patterns based on the context before or after the match, without including the context in the match itself.

Regex flags modify the behavior of the pattern matching. For example, the 'i' flag makes the match case-insensitive, and the 'm' flag enables multiline mode.

Tips and Best Practices

Always test your regular expressions on sample data before applying them to your entire dataset. Use online regex testers or the REGEXMATCH function in Google Sheets to validate your patterns.

If you need to match literal special characters (like '.', '\', or '+'), escape them with a backslash (\) to treat them as literal characters.

Usemage.com provides the =AI() function, which allows you to automate various spreadsheet tasks using artificial intelligence. This can be a powerful complement to regular expressions for data manipulation and analysis.

Want to automate your busy work in Google Sheets with AI?

Videos