To extract text matching a pattern in Google Sheets, use the REGEXEXTRACT function: =REGEXEXTRACT(text, regular_expression). For example, =REGEXEXTRACT("Order #12345", "#(\d+)") returns 12345. This function pulls the first substring that matches a regular expression, which is a sequence of characters defining a search pattern.
What Is REGEXEXTRACT and When to Use It
REGEXEXTRACT is one of three regex functions in Google Sheets, alongside REGEXMATCH (checks if a pattern exists) and REGEXREPLACE (replaces matches). It is most useful when you need to pull a specific piece of text from a larger string, such as extracting an email address, a date, a product code, or a number from a description. Unlike simple text functions like LEFT or MID, which rely on fixed positions, REGEXEXTRACT works based on the pattern of the text, making it flexible for irregular data.
For instance, if you have a list of URLs in column A and want the domain names, you can use =REGEXEXTRACT(A2, "https?://([^/]+)") to get everything after the protocol and before the first slash. This would turn https://www.example.com/page into www.example.com.
How to Use REGEXEXTRACT: Syntax and Examples
The syntax is straightforward: REGEXEXTRACT(text, regular_expression). The first argument is the text you want to search (a cell reference or a string in quotes), and the second is the pattern. If the pattern contains capturing groups (parentheses), REGEXEXTRACT returns the first capturing group by default. If there are multiple groups, you can specify which one to return by adding a third argument: =REGEXEXTRACT(text, pattern, group_number).
Here are practical examples:
- Extract the first number from a string:
=REGEXEXTRACT(A2, "\d+")– returns123fromItem 123. - Extract a date in YYYY-MM-DD format:
=REGEXEXTRACT(A2, "\d{4}-\d{2}-\d{2}")– returns2025-03-14fromDue: 2025-03-14. - Extract text between two markers:
=REGEXEXTRACT(A2, "Start(.*?)End")– returnsmiddlefromStart middle End.
Note that Google Sheets uses RE2 syntax for regular expressions, which is similar to most regex flavors but lacks some features like lookbehind assertions. For example, you cannot use (?<=...) to match text preceded by a pattern; instead, you would include the preceding context in the match and use a capturing group.
Choosing the Right Regular Expression
Writing a good regex requires understanding the structure of your data. Start by identifying the exact substring you need and what surrounds it. Use these building blocks:
\d– any digit (0-9);\d+means one or more digits.\w– any letter, digit, or underscore;\w+matches words..– any single character (except newline).*– zero or more of the preceding element;+– one or more;?– zero or one.[abc]– any character in the set;[^abc]– any character not in the set.(...)– capturing group;(?:...)– non-capturing group.^– start of string;$– end of string.
Test your regex on a few sample values before applying it to the whole column. You can use the REGEXMATCH function to check if a pattern matches, or simply copy the formula down and look for errors. A common mistake is forgetting to escape special characters like . or +; if you want a literal dot, use \..
Common Problems and How to Fix Them
Several issues arise frequently with REGEXEXTRACT:
- #N/A error – This means no match was found. Wrap the function in IFERROR to return a custom value:
=IFERROR(REGEXEXTRACT(A2, "pattern"), "Not found"). - Extracting too much or too little – Greedy matching. By default,
*and+are greedy, meaning they match as much as possible. Use.*?or.+?for lazy matching. For example, to extract the first number, use"\d+"– that is fine, but if you have"\d.*\d"it will match from the first to the last digit. Use\d+?to stop at the first. - Regex works in another tool but not in Sheets – Google Sheets uses RE2, which does not support lookbehind or backreferences. Rewrite the pattern using capturing groups instead.
- Extracting multiple matches – REGEXEXTRACT only returns the first match. To get all matches, you need to use a helper like SPLIT with REGEXREPLACE, or use a custom function via Google Apps Script. For example, to extract all numbers, you can use
=SPLIT(REGEXREPLACE(A2, "[^\d]+", ","), ",").
REGEXEXTRACT vs. Other Text Functions
While REGEXEXTRACT is powerful, sometimes simpler functions are better. For fixed-width data, LEFT, RIGHT, and MID are easier and faster. For finding the position of a character, FIND or SEARCH combined with MID can work. However, when the data is irregular or the pattern is complex, regex is the right choice.
For example, to extract the area code from a phone number like (555) 123-4567, you could use =MID(A2,2,3) if the format is always the same, but if the number sometimes has no parentheses, regex is safer: =REGEXEXTRACT(A2, "\(?(\d{3})\)?[- ]").
Using REGEXEXTRACT with Array Formulas
To apply REGEXEXTRACT to an entire column without dragging, use ARRAYFORMULA. For example, with data in A2:A, put this in B2: =ARRAYFORMULA(IFERROR(REGEXEXTRACT(A2:A, "pattern"))). This returns an array of results for each row. Note that REGEXEXTRACT works with arrays when wrapped in ARRAYFORMULA, but the pattern must be a single string, not an array.
Alternatively, in newer versions of Google Sheets, you can use the MAP function: =MAP(A2:A, LAMBDA(x, IFERROR(REGEXEXTRACT(x, "pattern")))). This is more flexible if you need to process each value individually.
Frequently Asked Questions
Can I extract multiple matches with REGEXEXTRACT? No, REGEXEXTRACT only returns the first match. To get all matches, you can use a combination of SPLIT and REGEXREPLACE, or write a custom function in Apps Script. For example, to extract all email addresses from a cell, you could use =SPLIT(REGEXREPLACE(A2, "[^\s@]+@[^\s@]+\.[^\s@]+", "|" & REGEXEXTRACT(A2, "[^\s@]+@[^\s@]+\.[^\s@]+")), "|"), but that is messy; a script is cleaner.
Why does my regex return #N/A even though the text looks right? Check for hidden characters like spaces or newlines. The dot in regex does not match newlines by default. Use [\s\S] to match any character including newlines. Also, ensure you have escaped special characters correctly.
Does REGEXEXTRACT work in Excel? No, Excel does not have REGEXEXTRACT. In Excel, you would use a combination of functions like FIND, MID, or use VBA with the RegExp object. In Microsoft 365, you can use the new TEXTBEFORE and TEXTAFTER functions for simpler extractions, but for complex patterns, you would need to use VBA or a third-party add-in.
Is REGEXEXTRACT case-sensitive? Yes, by default it is case-sensitive. To make it case-insensitive, you can use the inline modifier (?i) at the start of the pattern. For example, =REGEXEXTRACT(A2, "(?i)abc") matches ABC or abc.
Can I use REGEXEXTRACT to extract text between two specific words? Yes, use a lazy match: =REGEXEXTRACT(A2, "Start(.*?)End"). The .*? matches as little as possible, so it stops at the first occurrence of “End”. If you want the last occurrence, use .* (greedy) but that will match from the first “Start” to the last “End”.
Alternatives and Advanced Tips
If you find yourself writing very complex regex, consider splitting the task into multiple steps. Use REGEXEXTRACT to pull a segment, then apply another regex to that result. For example, to get the username from an email like john.doe@example.com, you could use =REGEXEXTRACT(A2, "(.*)@") to get john.doe, then further process with SUBSTITUTE to replace dots with spaces.
Another powerful tool is the REGEXREPLACE function, which can remove unwanted parts. For instance, to clean a phone number by removing all non-digits, use =REGEXREPLACE(A2, "\D", ""). This is often easier than extracting the digits directly.
For users who need to parse complex data regularly, consider using Google Apps Script to create a custom function that uses JavaScript's regex engine, which supports more features like lookbehind. However, for most spreadsheet tasks, REGEXEXTRACT with RE2 is sufficient.
Related
- Forecast x Excel: How to Forecast in Excel
- Subtotal Function in Google Sheets: Syntax & Examples
- HP Shortcut Keys: The Complete List You Need
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Create a Box Plot in Excel: Steps and InterpretationTo create a box plot in Excel for Microsoft 365 on Windows, put each group of numeric observations…
- How to Paste Exactly the Same in Excel Without ChangesTo paste a copy of cells in Excel with their contents and formatting, select the source cells, press…
- Excel Find and Replace in Selected CellsYes. In Excel for Microsoft 365 on Windows, select a multi-cell range, press Ctrl+H, enter the text to…
- How to Delete an Excel File from OneDrive SafelyTo delete an Excel file stored in OneDrive, close it, open OneDrive on the web, find the workbook,…
- Find Duplicate Entries in an Excel ColumnTo find duplicate entries in an Excel column, select the values and choose Home > Conditional Formatting >…
- Excel Slicer Best Practices for Clear, Reliable ReportsFor reliable Excel slicers, start with a clean Excel Table or PivotTable, insert slicers only for fields people…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.