The RegExpSearch function returns the position within a string where a regular expression is matched.
If a string matches a regular expression, RegExpSearch returns the position in the string of the first character that was matched. You can also say that it returns the base-1 index for the match, i.e. with the first character counted as 1. See below for an example.
In situations where you can use Excel’s Find function instead, it gives the same result, since Excel uses 1-based indices.
=RegExpSearch(”1”, ”1234”) returns 1 =Find(”1”, ”1234”) returns 1
If the “needle” does not match the “haystack”, RegExpSearch returns a #VALUE! error, just like Find.
There are many web pages that help you test and debug your regular expressions, e.g. regex101.com.
In this example, the regular expression in A2 is matched against the string in A4. There is a match and the function returns 6 which is the position of the first character of the match within the string.
This is what this RegExpSearch function call will look like:
=RegExpSearch(regex, string, flags)
The string is matched against the regular expression, controlled by the optional flags. The function and its results are modeled after the Find function in Excel:
Specify the regular expression you wish to match. Do not use any special delimiter for the regex, e.g. forward slash.
Specify the target string that you want to evaluate against the regular expression.
Specify the flags that control the evaluation of the regular expression (see below). The flags parameter is optional.
The way the function operates can be controlled by the flags below.
Global search, matches the pattern multiple times. Off by default.
Case insensitive search, both a and A match a. On by default.
Multi-line mode where ^ matches the start of the string and $ matches the end of the entire string. Without this, each line in the string is treated separately. On by default.
Short for single line; the . wildcard also matches newline characters. On by default.
Treats a pattern as a sequence of Unicode code points. On by default.
Perform a sticky search that matches starting at the current position in the target string. Off by default.