Google Sheets Function: REGEXEXTRACT
PREMIUM ACCESS
Support the site by becoming a Premium member and enjoy several exclusive options:
- Ad-free Navigation
- No Tracking Mode
- Incognito Mode (on the forum)
- Dark Mode
The REGEXEXTRACT function extracts one or more parts of a text using regular expressions.
Usage:
=REGEXEXTRACT(text, regular_expression)
Extracting the First Word Found
The REGEXEXTRACT function here returns the first word found among the two possibilities ("fun" or "terrifying"):
=REGEXEXTRACT(A2,"fun|terrifying")
Extracting a Reference
This time, the REGEXEXTRACT function returns the part of the reference number matching the expression
"[A-Z]\d{2}"
(i.e., an uppercase letter followed by 2 digits):
=REGEXEXTRACT(A2,"[A-Z]\d{2}")
Extracting a Number
The regex
"\d*"
corresponds to "all digits in unlimited quantity," meaning the function will extract all digits until encountering a non-digit character:
=REGEXEXTRACT(A2,"\d*")
Extracting at the End
To extract the digits at the end of the text this time, add a "$" to the end of the regex:
=REGEXEXTRACT(A2,"\d*$")
Extracting Multiple Parts
To extract the different groups of numbers, add parentheses around each part of the text to extract:
=REGEXEXTRACT(A2,"(\d*)-?(\d*)-?(\d*)-?(\d*)")
Extracting in the Middle
For example, to extract the second group of numbers, the regex is as follows:
=REGEXEXTRACT(A2,"(?:(\d*)-?){2}")
The addition of "?:", after the first parenthesis indicates it is non-capturing (so it won't return any value). There is then only one pair of parentheses left (thus only one value returned) and a quantifier of 2: