Number of characters from the beginning of the string where the function starts searching for matches.
The value must be a positive integer.
Default: 1 (the search for a match starts at the first character on the left)
occurrence
Specifies the first occurrence of the pattern from which to start returning matches.
The function skips the first occurrence - 1 matches. For example, if there are 5 matches and
you specify 3 for the occurrence argument, the function ignores the first two matches and
returns the third, fourth, and fifth matches.
Default: 1
option
Specifies whether to return the offset of the first character of the match (0) or the offset of the first character following the end of the match (1).
Default: 0
regexp_parameters
String of one or more characters that specifies the parameters used for searching for matches. Supported values:
Parameter
Description
c
Case-sensitive matching
i
Case-insensitive matching
m
Multi-line mode
e
Extract submatches
s
Single-line mode POSIX wildcard character . matches \n
By default, REGEXP_INSTR returns the begin or end character offset for the entire matching part of the subject.
However, if the e (for “extract”) parameter is specified, REGEXP_INSTR returns the begin or end
character offset for the part of the subject that matches the first sub-expression in the pattern.
If e is specified but a group_num is not also specified, then the group_num
defaults to 1 (the first group). If there is no sub-expression in the pattern, REGEXP_INSTR behaves as
if e was not set. For examples that use e, see Examples in this topic.
group_num
The group_num parameter specifies which group to extract. Groups are specified by using parentheses in
the regular expression.
If a group_num is specified, Snowflake allows extraction even if the e option was not
also specified. The e option is implied.
When the 2026_04 BCR bundle
is enabled, the function accepts arguments with a collation specification. The
collation has no effect on pattern matching: matching is always case-sensitive
unless you pass the i flag in the parameters argument.
Search for a matching string. In this case, the string is nevermore followed by a single decimal digit
(for example, nevermore1). The example uses the REGEXP_SUBSTR function to show the matching
substring:
+----+-------------------------------------+------------+----------+| ID | STRING1 |SUBSTRING|POSITION||----+-------------------------------------+------------+----------||1| nevermore1, nevermore2, nevermore3.| nevermore3 |25|+----+-------------------------------------+------------+----------+
This query is nearly identical the previous query, but this one shows how to use the option argument to
indicate whether you want the position of the matching expression, or the position of the first character after the
matching expression:
This section shows how to use the “group” feature of regular expressions.
The first few examples in this section don’t use capture groups. The section starts with some simple examples,
then continues with examples that use capture groups.
These examples use the strings created below:
CREATEORREPLACETABLE demo2 (id INT, string1 VARCHAR);INSERTINTO demo2 (id, string1)VALUES(2,'It was the best of times, it was the worst of times.'),(3,'In the string the extra spaces are redundant.'),(4,'A thespian theater is nearby.');SELECT*FROM demo2;
+----+-------------------------------------------------------------+| ID | STRING1 ||----+-------------------------------------------------------------||2| It was the best of times, it was the worst of times.||3|In the string the extra spaces are redundant.||4| A thespian theater is nearby.|+----+-------------------------------------------------------------+
The strings have the following characteristics:
The string with an id of 2 has multiple occurrences of the word “the”.
The string with an id of 3 has multiple occurrences of the word “the” with extra blank spaces
between the words.
The string with an id of 4 has the character sequence “the” inside multiple words (“thespian”
and “theater”), but without the word “the” by itself.
This example looks for the first occurrence of the word the, followed by one or more non-word characters (for example,
the whitespace separating words), followed by one or more word characters.
“Word characters” include not only the letters a-z and A-Z, but also the
underscore (“_”) and the decimal digits 0-9, but not whitespace, punctuation, and so on.
+----+-------------------------------------------------------------+--------------+----------+| ID | STRING1 |SUBSTRING|POSITION||----+-------------------------------------------------------------+--------------+----------||2| It was the best of times, it was the worst of times.| the best |8||3|In the string the extra spaces are redundant.| the string|7||4| A thespian theater is nearby.|NULL|0|+----+-------------------------------------------------------------+--------------+----------+
Starting from position 1 of the string, look for the second occurrence of the word the,
followed by one or more non-word characters, followed by one or more word characters.
+----+-------------------------------------------------------------+-------------+----------+| ID | STRING1 |SUBSTRING|POSITION||----+-------------------------------------------------------------+-------------+----------||2| It was the best of times, it was the worst of times.| the worst |34||3|In the string the extra spaces are redundant.| the extra |22||4| A thespian theater is nearby.|NULL|0|+----+-------------------------------------------------------------+-------------+----------+
This example is similar to the preceding example, but adds capture groups. Rather than returning the position of the
entire match, this query returns the position of only the group, which is the portion of the substring that matches the
part of the regular expression in parentheses. In this case, the returned value is the position of the word
after the second occurrence of the word the.
+----+-------------------------------------------------------------+-----------+----------+| ID | STRING1 |SUBSTRING|POSITION||----+-------------------------------------------------------------+-----------+----------||2| It was the best of times, it was the worst of times.| worst |38||3|In the string the extra spaces are redundant.| extra |28||4| A thespian theater is nearby.|NULL|0|+----+-------------------------------------------------------------+-----------+----------+
If you specify the 'e' (extract) parameter, but don’t specify the group_num, then the group_num
defaults to 1:
+----+-------------------------------------------------------------+-----------+----------+| ID | STRING1 |SUBSTRING|POSITION||----+-------------------------------------------------------------+-----------+----------||2| It was the best of times, it was the worst of times.| worst |38||3|In the string the extra spaces are redundant.| extra |28||4| A thespian theater is nearby.|NULL|0|+----+-------------------------------------------------------------+-----------+----------+
If you specify a group_num, Snowflake assumes that you want to extract, even if you didn’t specify
'e' (extract) as one of the parameters:
+----+-------------------------------------------------------------+-----------+----------+| ID | STRING1 |SUBSTRING|POSITION||----+-------------------------------------------------------------+-----------+----------||2| It was the best of times, it was the worst of times.| worst |38||3|In the string the extra spaces are redundant.| extra |28||4| A thespian theater is nearby.|NULL|0|+----+-------------------------------------------------------------+-----------+----------+
This example shows how to retrieve the position of second word from the first, second, and third matches of
a two-word pattern in which the first word is A. This also shows that trying to go beyond the last
pattern causes Snowflake to return 0.
Create a table and insert data:
CREATETABLE demo3 (id INT, string1 VARCHAR);INSERTINTO demo3 (id, string1)VALUES(5,'A MAN A PLAN A CANAL');
+----+----------------------+------------+-----------+------------+-----------+------------+-----------+------------+-----------+| ID | STRING1 | SUBSTRING1 | POSITION1 | SUBSTRING2 | POSITION2 | SUBSTRING3 | POSITION3 | SUBSTRING4 | POSITION4 ||----+----------------------+------------+-----------+------------+-----------+------------+-----------+------------+-----------||5| A MAN A PLAN A CANAL | MAN |3|PLAN|9| CANAL |16|NULL|0|+----+----------------------+------------+-----------+------------+-----------+------------+-----------+------------+-----------+
This example shows how to retrieve the position of first, second, and third groups within the first occurrence of the pattern.
In this case, the returned values are the positions of the individual letters of the word MAN.
+----+----------------------+------------+-----------+------------+-----------+------------+-----------+| ID | STRING1 | SUBSTRING1 | POSITION1 | SUBSTRING2 | POSITION2 | SUBSTRING3 | POSITION3 ||----+----------------------+------------+-----------+------------+-----------+------------+-----------||5| A MAN A PLAN A CANAL | M |3| A |4| N |5|+----+----------------------+------------+-----------+------------+-----------+------------+-----------+
The following example matches occurrences of the word was. Matching begins at the first character in the string
and returns the position in the string of the character following the first occurrence:
SELECTREGEXP_INSTR('It was the best of times, it was the worst of times','\\bwas\\b',1,1)ASresult;
+--------+|RESULT||--------||4|+--------+
The following example returns the offset of the first character of the part of the string that matches the
pattern. Matching begins at the first character in the string and returns the first occurrence of the pattern:
SELECTREGEXP_INSTR('It was the best of times, it was the worst of times','the\\W+(\\w+)',1,1,0)ASresult;
+--------+|RESULT||--------||8|+--------+
The following example is the same as the previous example, but uses the e parameter to return the
character offset for the part of the subject that matches the first subexpression in the pattern (the
first set of word characters after the):
SELECTREGEXP_INSTR('It was the best of times, it was the worst of times','the\\W+(\\w+)',1,1,0,'e')ASresult;
+--------+|RESULT||--------||12|+--------+
The following example matches occurrences of words ending in st preceded by two or more alphabetic characters
(case-insensitive). Matching begins at the fifteenth character in the string and returns the position in the string of
the character following the first occurrence (the beginning of worst):
SELECTREGEXP_INSTR('It was the best of times, it was the worst of times','[[:alpha:]]{2,}st',15,1)ASresult;
+--------+|RESULT||--------||38|+--------+
To run the next set of examples, create a table and insert data:
CREATEORREPLACETABLE message(bodyVARCHAR(255));INSERTINTO message VALUES('Hellooo World'),('How are you doing today?'),('the quick brown fox jumps over the lazy dog'),('PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS');
Return the offset of the first character in the first match that contains a
lowercase o:
+---------------------------------------------+--------+|BODY|RESULT||---------------------------------------------+--------|| Hellooo World |1|| How are you doing today?|1|| the quick brown fox jumps over the lazy dog |11|| PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS |0|+---------------------------------------------+--------+
Return the offset of the first character in the first match that contains a
lowercase o, starting at the third character in the subject:
+---------------------------------------------+--------+|BODY|RESULT||---------------------------------------------+--------|| Hellooo World |3|| How are you doing today?|9|| the quick brown fox jumps over the lazy dog |11|| PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS |0|+---------------------------------------------+--------+
Return the offset of the first character in the third match that contains a
lowercase o, starting at the third character in the subject:
+---------------------------------------------+--------+|BODY|RESULT||---------------------------------------------+--------|| Hellooo World |0|| How are you doing today?|19|| the quick brown fox jumps over the lazy dog |27|| PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS |0|+---------------------------------------------+--------+
Return the offset of the last character in the third match that contains a
lowercase o, starting at the third character in the subject:
+---------------------------------------------+--------+|BODY|RESULT||---------------------------------------------+--------|| Hellooo World |0|| How are you doing today?|24|| the quick brown fox jumps over the lazy dog |31|| PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS |0|+---------------------------------------------+--------+
Return the offset of the last character in the third match that contains a
lowercase o, starting at the third character in the subject, with case-insensitive matching:
+---------------------------------------------+--------+|BODY|RESULT||---------------------------------------------+--------|| Hellooo World |0|| How are you doing today?|24|| the quick brown fox jumps over the lazy dog |31|| PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS |35|+---------------------------------------------+--------+