Extract text with regex pattern
=REGEXEXTRACT(text, pattern, [return_mode], [case_sensitivity])
| Parameter | Description |
|---|---|
text |
The text value to extract from. |
pattern |
The pattern to extract. |
return_mode |
[optional] 0 = first match, 1 = all matches, 2 = capture groups. |
case_sensitivity |
[optional] 0 = Case sensitive, 1= Case-insensitive. Default is 0. |
The REGEXEXTRACT function provides a way to extract text values using regular expressions. To use REGEXEXTRACT, provide the text string to extract fro
=REGEXEXTRACT("10 apples","[0-9]+") // returns "10"
You can see how this works in the worksheet below, where the formula in cell D5, copied down, is:
=REGEXEXTRACT(B5,"[0-9]+")
Expanding on the example above, the worksheet below shows how to match and extract phone numbers in the format xxx-xxx-xxxx (i.e., a number like 888-1
=REGEXEXTRACT(B5,"\d{3}-\d{3}-\d{4}")
Another classic use of regex is matching and extracting email addresses. In the worksheet below, the formula in cell D5, copied down, is:
=REGEXEXTRACT(B5, "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")
Another classic problem in Excel is how to extract a date from a text string. Traditionally, you might use a formula based on the SEARCH function and
=MID(A1,SEARCH("??/??/??",A1),8)+0
However, SEARCH only supports Excel's very primitive wildcards, so the formula above is error-prone. With REGEXEXTRACT, we can use a more robust formu
=REGEXEXTRACT(A1,"\b\d{1,2}/\d{1,2}/\d{2,4}\b")+0