
Google Sheets’ REGEXTRACT is a powerful and robust function that extracts matching substrings from a string/text. Excel offers multiple text functions to extract specific text from a string, but they don’t have the flexibility of regex. REGEXTRACT function is only available in Google Sheets. This post will show the use and differences between REGEXTRACT and Excel’s text functions along with practical examples.
What is REGEXEXTRACT Function?
The REGEXEXTRACT function uses regular expressions (regex) to extract matching substrings from a string. It returns the first match of the pattern. For complex pattern matching regular expressions are the most efficient tool, they allow you to search patterns to capture the exact text you need. It’s easy to use for moderate to large data.
Syntax:
=REGEXEXTRACT(text, regular_expression)
- text: the text of the cell or range from which you want to extract substrings matching the pattern.
- regular_expression: it specifies the patterns to search for the exact matching within the text.
What are Text Functions in Excel?
Excel’s text functions help to modify, format, transform, and manipulate text strings. By using the combination of these functions, you can extract specific text from string values. Commonly used text functions are MID, LEFT, RIGHT, FIND, SEARCH, etc to extract specific text from a known position within the formula.
Using REGEXEXTRACT and Text Functions in Excel with Customer Data
To show you the use of both REGEXEXTRACT and text functions consider user data. Suppose you want to extract the area code of the customer from the phone number.
Sample Data:
| Name | Phone Number | Area Code | |
| John Doe | john.doe@example.com | +1-415-123-4567 | |
| Jane Smith | jane_smith@web.net | +1-555-123-3568 | |
| Mike Jones | mike.jones@example.org | +1-213-398-9169 | |
| Sarah Lee | sarah_lee@xyz.com | +1-312-732-1570 | |
| Emily Wong | emily_wong@webservice.co | +1-206-345-6789 |
REGEXTRACT function in Google Sheets:
To extract the area code from the phone number in Google Sheets, you will need to insert the following formula in cell D2.
Formula:
=REGEXEXTRACT(C2, “\+1-(\d{3})-“)
Explanation:
- C2: It’s the cell reference of the phone number text.
- “\+1-(\d{3})-“: The regular expression of the pattern matches the text in C2 to extract the area code.
- \+1- It matches the initial plus (+1) and hyphens in the string.
- (\d{3}) extracts the exact next three digits, which are 415.
- – It matches the following hyphen of the captured text.
Text Functions in Excel:
To extract the area code from the phone number text in Excel, you can use the MID function. It extracts a fixed number of characters from a text, starting at a fixed position. To use this function, you must know the exact number of characters and start position. Select cell D2 and insert the following formula.
Formula:
=MID(C2, 4, 3)
Explanation: The MID function will extract the area code from the phone number text but to use this function you must know the exact number of positions you want to extract.
- C2: This is the cell reference of the phone number text.
- 4: Index number of the phone number text from which it starts extracting text. Here, it will start extracting text from the 4th character.
- 3: It’s the number of characters to extract. Starting from the 4th character it will extract 3 characters.
Output:
You will get the area code in column D of Google Sheets and Excel. Both will give the same result.
| Name | Phone Number | Area Code | |
| John Doe | john.doe@example.com | +1-415-123-4567 | 415 |
| Jane Smith | jane_smith@web.net | +1-555-123-3568 | 555 |
| Mike Jones | mike.jones@example.org | +1-213-398-9169 | 213 |
| Sarah Lee | sarah_lee@xyz.com | +1-312-732-1570 | 312 |
| Emily Wong | emily_wong@webservice.co | +1-206-345-6789 | 206 |
The REGEXTRACT is a dynamic function that serves different purposes while extracting matching text. It can create patterns for complex text extraction. It helps to clean and transform datasets, especially in web scraping. The regular expression can extract a matching string regardless of its position and format. On the other hand, the text functions (MID, LEFT, RIGHT, FIND, SEARCH, etc) are straightforward functions that can extract, and modify matching text from structured and predictable data. A combination of these functions works better while extracting specific text from a known position. Extracting matching text from complex text creates a large formula that will need multiple steps to complete.
Conclusion
In a nutshell, Google Sheets’ REGEXTRACT function will allow you to handle complex text patterns through regular expression. If you are familiar with regex this will simplify your formula and give you a dynamic solution. To avoid pattern matching you can use Excel’s text functions for basic extractions where the data is consistent and predictable. Excel text functions are easy to use for simple data.
