// strings reference

SQL REGEXP_INSTR function

Returns the 1-based position where a regular expression matches a string, or 0 when there is no match.

Категория
Strings
Returns
integer
In this category
93 of 172

Синтаксис

REGEXP_INSTR(str, pattern [, start [, occurrence [, return_opt [, flags [, group]]]]])

Поддержка движка

ДвижокSupportЗаметки
SQL Server Да SQL Server 2025 and Azure SQL Database.
PostgreSQL Да PostgreSQL 15 and later.
MySQL Да MySQL 8.0.4 and later; no capture-group argument.
SQLite Not built in No REGEXP_INSTR; the REGEXP operator only answers true or false.
Oracle Да Supported under this name.

Примеры

Oracle

SELECT REGEXP_INSTR(phone, '[0-9]{3}') AS first_digits
FROM contacts;

MySQL

SELECT REGEXP_INSTR('abc123', '[0-9]+') AS pos;  -- 4

PostgreSQL

SELECT regexp_instr('abc123', '[0-9]+', 1, 1, 1) AS after_match;  -- 7

Полезно знать

  • Regex flavours differ: Oracle uses POSIX extended syntax with Perl-style shortcuts such as \d, MySQL uses ICU, PostgreSQL its own ARE engine and SQL Server 2025 RE2. Classes, quantifiers and alternation port; lookarounds and backreferences do not.
  • return_opt 0 (the default) gives the position of the first matched character, 1 the position just after the match. Oracle, PostgreSQL and SQL Server also take a capture-group number after the flags; MySQL stops at the flags.
  • Every engine returns 0 for no match and NULL when the string or pattern is NULL; positions count characters from 1.

Questions about REGEXP_INSTR

What is the difference between REGEXP_INSTR and REGEXP_SUBSTR?

REGEXP_INSTR says where the match is; REGEXP_SUBSTR returns the matched text. They take the same start and occurrence arguments, so you can pair them.

How do I get the end position of a regex match?

Set the return option to 1: REGEXP_INSTR(str, pattern, 1, 1, 1) returns the position just after the first match. The argument order is the same in all four engines.

Which regex flavor does each engine use?

Oracle: POSIX extended plus Perl-style escapes. MySQL 8: ICU. PostgreSQL: its Advanced Regular Expressions. SQL Server 2025: RE2, which has no backreferences or lookarounds. Test anything beyond classes, anchors and quantifiers on each engine.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.