Google Sheets supports regular expressions (regex) through three dedicated functions—REGEXMATCH, REGEXEXTRACT, and REGEXREPLACE—which let you search, extract, and replace text patterns in cells. For example, =REGEXEXTRACT(A1, "[0-9]+") pulls the first number from cell A1. These functions work in any web browser and are also available in the Google Sheets mobile apps, though they differ from Excel’s regex support (Excel uses REGEXTEST, REGEXEXTRACT, and REGEXREPLACE only in Excel 365, with slightly different syntax).
What Are Regular Expressions?
A regular expression (regex) is a sequence of characters that defines a search pattern. Instead of manually scanning or using multiple nested functions, you can use regex to match patterns like email addresses, phone numbers, dates, or any text that follows a rule. In Google Sheets, regex is powered by the RE2 syntax, which is a fast, safe regex engine developed by Google. RE2 supports most common regex features but does not support backreferences or lookarounds (like (?=...)).
The Three Regex Functions in Google Sheets
Google Sheets offers three functions that accept a regular expression. Each works on a text string and returns a different result.
REGEXMATCH(text, regex)– Returns TRUE if the pattern matches anywhere in the text, otherwise FALSE. Example:=REGEXMATCH("hello123", "[0-9]+")returns TRUE.REGEXEXTRACT(text, regex)– Returns the first substring that matches the pattern. If the regex contains capturing groups (parentheses), it returns the first group. Example:=REGEXEXTRACT("Order #123", "#([0-9]+)")returns123.REGEXREPLACE(text, regex, replacement)– Replaces every match of the pattern with a replacement string. Example:=REGEXREPLACE("abc123", "[0-9]+", "#")returnsabc#.
These functions are case-sensitive by default. To make them case-insensitive, add (?i) at the start of the pattern, like (?i)apple.
How to Write a Regex Pattern
Regex patterns are built from literal characters and metacharacters. Here are the most useful ones for Google Sheets:
.– any single character (except newline)*– zero or more of the previous character+– one or more of the previous character?– zero or one of the previous character[]– any one character inside brackets (e.g.,[a-z]for lowercase letters)[^]– any character not inside brackets^– start of the string$– end of the string\d– any digit (same as[0-9])\w– any letter, digit, or underscore\s– any whitespace (space, tab, newline)|– OR operator (e.g.,cat|dog)()– capturing group
For example, to match a US phone number like (555) 123-4567, you could use \(\d{3}\) \d{3}-\d{4}. To match an email address, a simple pattern is [A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}.
Practical Examples in Sheets
Let’s say you have a list of product codes in column A, like AB-123, CD-456, and you want to extract the numbers. In cell B1, you could enter:
=REGEXEXTRACT(A1, "[0-9]+")
This returns 123 for the first row. To replace all hyphens with a colon, use:
=REGEXREPLACE(A1, "-", ":")
To check if a cell contains a date in the format MM/DD/YYYY, use:
=REGEXMATCH(A1, "\d{2}/\d{2}/\d{4}")
These functions are often combined with ARRAYFORMULA to apply to an entire column at once. For instance, to extract numbers from every cell in A2:A, you could put in B2: =ARRAYFORMULA(REGEXEXTRACT(A2:A, "[0-9]+")).
Common Pitfalls and How to Avoid Them
Regex in Google Sheets can be tricky. Here are the most frequent issues and solutions:
- Escaping backslashes – In Google Sheets, you write a backslash as
\inside a string. So\dis correct, not\. - No lookarounds – RE2 does not support lookahead or lookbehind. If you need to match text after a prefix, use capturing groups or
REGEXEXTRACTwith a group. - Greedy matching – Patterns like
.*are greedy and may match more than expected. Use.*?for non-greedy matching if needed. - Newlines – The
.does not match newline characters. If your text has line breaks, use[\s\S]to match any character including newline. - Error with no match –
REGEXEXTRACTreturns a#N/Aerror if no match is found. Wrap it inIFERRORto handle gracefully:=IFERROR(REGEXEXTRACT(A1, "[0-9]+"), "No number").
Regex vs. Other Text Functions
Google Sheets also has non-regex functions like FIND, SEARCH, LEFT, RIGHT, and MID that can handle simple text extraction. Regex is more powerful and concise for complex patterns, but it can be slower on very large datasets. For simple tasks like finding a fixed substring, SEARCH (case-insensitive) or FIND (case-sensitive) are faster and easier to read. For example, to check if a cell contains “apple”, =ISNUMBER(SEARCH("apple", A1)) is simpler than =REGEXMATCH(A1, "apple"), but regex allows you to match variations like “Apple” or “APPLE” with (?i)apple.
Using Regex in Conditional Formatting and Filters
Regex is not limited to formulas. You can use it in conditional formatting rules to highlight cells that match a pattern. Select your range, go to Format > Conditional formatting, choose “Custom formula is” under the drop-down, and enter a formula like =REGEXMATCH(A1, "^[0-9]+") to highlight cells that start with a number. Similarly, you can use regex in the filter view’s “Filter by condition” by selecting “Custom formula is” and entering a regex formula. This allows you to filter rows where a column matches a pattern, such as all email addresses containing “gmail.com”.
Frequently Asked Questions
Can I use regex in Google Sheets on mobile? Yes, the functions work in the Google Sheets mobile apps, but typing complex patterns on a small keyboard can be tedious. You can still copy and paste patterns.
Why is my regex returning #N/A? REGEXEXTRACT returns #N/A when there is no match. Check that your pattern is correct and that the text doesn’t have hidden characters like spaces. Use TRIM if needed.
How do I make regex case-insensitive? Add (?i) at the start of the pattern. For example, (?i)hello matches “Hello”, “HELLO”, etc.
Does Google Sheets support lookahead or lookbehind? No, RE2 does not support lookarounds. You can often work around this by using capturing groups and REGEXEXTRACT.
Can I use regex in data validation? No, data validation does not support regex directly. You can use a custom formula in data validation that calls REGEXMATCH, but the rule must be a formula that returns TRUE for valid input.
What is the difference between REGEXEXTRACT and REGEXREPLACE? REGEXEXTRACT returns a matched substring, while REGEXREPLACE replaces matches with a new string and returns the entire modified text.
Alternatives and When to Use Them
If regex feels overwhelming, consider using the Split text to columns feature (Data > Split text to columns) for simple delimiters like commas or spaces. For more complex extraction, you can also use SPLIT, INDEX, and SEARCH combinations. However, regex is the most flexible and concise for patterns like phone numbers, zip codes, or log formats. For heavy data cleaning, you might also export to a tool like Python or use Google Apps Script for more advanced regex operations, but the built-in functions handle most everyday needs.
Related
- JavaScript Get Random Number: Math.random() Explained
- Cool Shortcut Keys: 30+ Time-Savers for Windows
- Data Entry Job Information: Work, Pay & Safety
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- PowerPoint Video Embed Code: How to Add and Play VideosTo use video embed code in PowerPoint, copy the full embed code from a supported video site, then…
- Excel Shortcut Keys for Faster Work on Windows and MacExcel shortcut keys let you perform common tasks with the keyboard instead of navigating menus. In Excel for…
- Excel Data Tables: What They Do and How to Use ThemIn Microsoft Excel, a Data Table is a What-If Analysis tool that recalculates a formula for different values…
- Excel Power Query Training: Skills, Courses, and PracticeExcel Power Query training teaches you to import, clean, combine, and refresh data in Excel without repeating the…
- Enum in SQL Server: How to Use It & Why It Doesn’t ExistSQL Server does not have an ENUM data type like MySQL or PostgreSQL. Instead, you enforce a fixed…
- Google Sheets TIMEVALUE Function: How to Use ItThe Google Sheets TIMEVALUE function converts a time written as text (like “3:30 PM”) into a decimal fraction…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.