REGEXP_INSTR
Returns the position of a regular expression match in a string.
PostgreSQL 17.9
REGEXP_INSTR(string, pattern[, start[, occurrence[, endoption[, flags[, subexpr]]]]])- [ ]optional part
- string
- String to search intext
- pattern
- Regular expressiontext
- start
- Position to start the search from, 1 by defaultintegerOptional
- occurrence
- Number of the match, 1 by defaultintegerOptional
- endoption
- 0 for the position where the match starts (by default), 1 for the position right after it endsintegerOptional01
- flags
- Flags, for example i to ignore casetextOptionalbceimnpqstwx
- subexpr
- Number of the group in parentheses whose position to return; 0 by default, the whole matchintegerOptional
Return value
An integer position starting from 1; 0 if there is no match
Examples
Match positions
PostgreSQL 17.9
SELECT REGEXP_INSTR('a1b22c333', '[0-9]+') AS FIRST,
REGEXP_INSTR('a1b22c333', '[0-9]+', 1, 2) AS SECOND,
REGEXP_INSTR('a1b22c333', '[0-9]+', 1, 2, 1) AS after_second,
REGEXP_INSTR('abc', '[0-9]') AS not_found;Where the last name starts
PostgreSQL 17.9
SELECT member_name,
REGEXP_INSTR(member_name, '[A-Z]', 1, 2) AS last_name_start
FROM FamilyMembers;Details
The function appeared in PostgreSQL 15. Positions start from 1. To find a plain substring without a pattern, STRPOS or POSITION is simpler.