The RegExpSearch function

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.

Learn more about regular expressions

Regular expressions are well described on the web, but there is not much Excel-specific information since Excel does not support regular expressions. One good starting point is the user guide for JavaScript in the Firefox browser.

There are many web pages that help you test and debug your regular expressions, e.g. regex101.com.

Function reference

Screenshot of the RegExpSearch function

Example

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(A2,A4,A8)

Format and parameters

=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:

  • For a match, the function returns the position within the string where the first match begins. You can also say it returns the 1-based index for the first matching character, with the left-most character in the string counted as 1.
  • If the regex is empty or missing, the function returns 1.
  • If a regex is present but the string is empty or missing, the function returns a #VALUE! error.
  • If both a regex and a string are present, but there is no match for the regex within the string, the function returns a #VALUE! error.

regex

Specify the regular expression you wish to match. Do not use any special delimiter for the regex, e.g. forward slash.

string

Specify the target string that you want to evaluate against the regular expression.

flags

Specify the flags that control the evaluation of the regular expression (see below). The flags parameter is optional.

Flags with regular expressions

The way the function operates can be controlled by the flags below.

  • If you include a flag in the string, it is turned On.
  • If you omit a flag from the string, it is turned Off.
  • If you provide an empty string, all flags are turned Off.
  • If you don’t provide a flag string, the default flags are imsu

g

Global search, matches the pattern multiple times. Off by default.

i

Case insensitive search, both a and A match a. On by default.

m

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.

s

Short for single line; the . wildcard also matches newline characters. On by default.

u

Treats a pattern as a sequence of Unicode code points. On by default.

y

Perform a sticky search that matches starting at the current position in the target string. Off by default.