REGEX_SEARCH

Returns the index of the first character of the first matching substring based on a regular expression pattern

Description

The REGEX_SEARCH() function is used to find and return the starting index of the first character of the first occurrence of a substring that matches a regular expression pattern within a given text. This function is particularly useful for locating specific patterns or positions within text data.

Syntax

REGEX_SEARCH(text, pattern, [offset])

  1. text: The input text string in which you want to search for a matching substring.

  2. pattern: The regular expression pattern to search for within the text.

  3. offset: (Optional) An offset (number greater than 0) that specifies the occurrence of the matching substring to return. The default is usually the first occurrence (offset 1).

When the REGEX_SEARCH() function returns -1, it indicates that there is no matching occurrence of the specified regular expression pattern or offset within the given text.

For example, if a user is looking for specific data and REGEX_SEARCH() function returns -1, they might conclude that the desired data is not present in the document.

Example

Table

ID
Text

1

Contact us at (555) 555-1234 for support.

2

Please call 1-800-123-4567 for inquiries.

3

No phone number in this text.

Function

REGEX_SEARCH(`Text`, "[+]?[0-9]?[-. ]?\\(?[0-9]{3}\\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}")

Results

REGEX_SEARCH (integer)

15

11

-1

Last updated