Using Regular Expressions

1. Pattern Matching

OperatorBehavior
~Case-sensitive match
~*Case-insensitive match
!~ / !~*Negated
SIMILAR TOSQL-standard, like LIKE++
SELECT * FROM logs WHERE message ~* '\merror\M';

2. Using REGEXP_MATCH Function

SELECT regexp_match('user@example.com', '^([^@]+)@(.+)

      

3. Using REGEXP_MATCHES Function

SELECT regexp_matches('aaa-bbb-ccc', '\w+', 'g');  -- 3 rows
FlagMeaning
gGlobal (all matches)
iCase-insensitive
nNewline-sensitive
xIgnore whitespace in pattern

4. Using REGEXP_REPLACE Function

SELECT regexp_replace('Phone: 555-1234', '\d', '*', 'g');

5. Using REGEXP_SPLIT_TO_TABLE Function

SELECT regexp_split_to_table('one,two;three', '[,;]') AS token;

6. Using REGEXP_SPLIT_TO_ARRAY Function

SELECT regexp_split_to_array('one,two;three', '[,;]');  -- {one,two,three}

7. Using Capture Groups

SELECT (regexp_match('John Doe, 42', '^(\w+) (\w+), (\d+)

      

8. Using Flags

FlagEffect
iCase-insensitive
gGlobal
s. matches newline
m^/$ per line

9. Using Character Classes

ClassMatches
\d / [[:digit:]]Digit
\w / [[:alnum:]_]Word char
\s / [[:space:]]Whitespace
[^...]Negated class

10. Using Anchors

AnchorMatch
^String start (or line with 'm')
$String end
\m / \MWord start / end
\yWord boundary

11. Using REGEXP_COUNT Function

SELECT regexp_count('aaa bbb aaa', 'aaa');   -- 2 (PG 15+)

12. Using REGEXP_INSTR Function

SELECT regexp_instr('foo bar baz', 'b\w+', 1, 2);   -- 9  (PG 15+)

13. Using REGEXP_SUBSTR Function

SELECT regexp_substr('id=42 type=admin', 'id=(\d+)', 1, 1, '', 1);   -- '42'
Note: regexp_count, regexp_instr, regexp_substr, regexp_like added in PG 15 for Oracle compatibility.
);
-- {user,example.com}

3. Using REGEXP_MATCHES Function

SELECT regexp_matches('aaa-bbb-ccc', '\w+', 'g');  -- 3 rows
FlagMeaning
gGlobal (all matches)
iCase-insensitive
nNewline-sensitive
xIgnore whitespace in pattern

4. Using REGEXP_REPLACE Function

SELECT regexp_replace('Phone: 555-1234', '\d', '*', 'g');

5. Using REGEXP_SPLIT_TO_TABLE Function

SELECT regexp_split_to_table('one,two;three', '[,;]') AS token;

6. Using REGEXP_SPLIT_TO_ARRAY Function

SELECT regexp_split_to_array('one,two;three', '[,;]');  -- {one,two,three}

7. Using Capture Groups

SELECT (regexp_match('John Doe, 42', '^(\w+) (\w+), (\d+)$'))[1] AS first,
       (regexp_match('John Doe, 42', '^(\w+) (\w+), (\d+)$'))[3] AS age;

8. Using Flags

FlagEffect
iCase-insensitive
gGlobal
s. matches newline
m^/$ per line

9. Using Character Classes

ClassMatches
\d / [[:digit:]]Digit
\w / [[:alnum:]_]Word char
\s / [[:space:]]Whitespace
[^...]Negated class

10. Using Anchors

AnchorMatch
^String start (or line with 'm')
$String end
\m / \MWord start / end
\yWord boundary

11. Using REGEXP_COUNT Function

SELECT regexp_count('aaa bbb aaa', 'aaa');   -- 2 (PG 15+)

12. Using REGEXP_INSTR Function

SELECT regexp_instr('foo bar baz', 'b\w+', 1, 2);   -- 9  (PG 15+)

13. Using REGEXP_SUBSTR Function

SELECT regexp_substr('id=42 type=admin', 'id=(\d+)', 1, 1, '', 1);   -- '42'
Note: regexp_count, regexp_instr, regexp_substr, regexp_like added in PG 15 for Oracle compatibility.
))[1] AS first,
(regexp_match('John Doe, 42', '^(\w+) (\w+), (\d+)

8. Using Flags

FlagEffect
iCase-insensitive
gGlobal
s. matches newline
m^/$ per line

9. Using Character Classes

ClassMatches
\d / [[:digit:]]Digit
\w / [[:alnum:]_]Word char
\s / [[:space:]]Whitespace
[^...]Negated class

10. Using Anchors

AnchorMatch
^String start (or line with 'm')
$String end
\m / \MWord start / end
\yWord boundary

11. Using REGEXP_COUNT Function

SELECT regexp_count('aaa bbb aaa', 'aaa');   -- 2 (PG 15+)

12. Using REGEXP_INSTR Function

SELECT regexp_instr('foo bar baz', 'b\w+', 1, 2);   -- 9  (PG 15+)

13. Using REGEXP_SUBSTR Function

SELECT regexp_substr('id=42 type=admin', 'id=(\d+)', 1, 1, '', 1);   -- '42'
Note: regexp_count, regexp_instr, regexp_substr, regexp_like added in PG 15 for Oracle compatibility.
);
-- {user,example.com}

3. Using REGEXP_MATCHES Function

SELECT regexp_matches('aaa-bbb-ccc', '\w+', 'g');  -- 3 rows
FlagMeaning
gGlobal (all matches)
iCase-insensitive
nNewline-sensitive
xIgnore whitespace in pattern

4. Using REGEXP_REPLACE Function

SELECT regexp_replace('Phone: 555-1234', '\d', '*', 'g');

5. Using REGEXP_SPLIT_TO_TABLE Function

SELECT regexp_split_to_table('one,two;three', '[,;]') AS token;

6. Using REGEXP_SPLIT_TO_ARRAY Function

SELECT regexp_split_to_array('one,two;three', '[,;]');  -- {one,two,three}

7. Using Capture Groups

SELECT (regexp_match('John Doe, 42', '^(\w+) (\w+), (\d+)$'))[1] AS first,
       (regexp_match('John Doe, 42', '^(\w+) (\w+), (\d+)$'))[3] AS age;

8. Using Flags

FlagEffect
iCase-insensitive
gGlobal
s. matches newline
m^/$ per line

9. Using Character Classes

ClassMatches
\d / [[:digit:]]Digit
\w / [[:alnum:]_]Word char
\s / [[:space:]]Whitespace
[^...]Negated class

10. Using Anchors

AnchorMatch
^String start (or line with 'm')
$String end
\m / \MWord start / end
\yWord boundary

11. Using REGEXP_COUNT Function

SELECT regexp_count('aaa bbb aaa', 'aaa');   -- 2 (PG 15+)

12. Using REGEXP_INSTR Function

SELECT regexp_instr('foo bar baz', 'b\w+', 1, 2);   -- 9  (PG 15+)

13. Using REGEXP_SUBSTR Function

SELECT regexp_substr('id=42 type=admin', 'id=(\d+)', 1, 1, '', 1);   -- '42'
Note: regexp_count, regexp_instr, regexp_substr, regexp_like added in PG 15 for Oracle compatibility.
))[3] AS age;

8. Using Flags

FlagEffect
iCase-insensitive
gGlobal
s. matches newline
m^/$ per line

9. Using Character Classes

ClassMatches
\d / [[:digit:]]Digit
\w / [[:alnum:]_]Word char
\s / [[:space:]]Whitespace
[^...]Negated class

10. Using Anchors

AnchorMatch
^String start (or line with 'm')
$String end
\m / \MWord start / end
\yWord boundary

11. Using REGEXP_COUNT Function

SELECT regexp_count('aaa bbb aaa', 'aaa');   -- 2 (PG 15+)

12. Using REGEXP_INSTR Function

SELECT regexp_instr('foo bar baz', 'b\w+', 1, 2);   -- 9  (PG 15+)

13. Using REGEXP_SUBSTR Function

SELECT regexp_substr('id=42 type=admin', 'id=(\d+)', 1, 1, '', 1);   -- '42'
Note: regexp_count, regexp_instr, regexp_substr, regexp_like added in PG 15 for Oracle compatibility.
); -- {user,example.com}

3. Using REGEXP_MATCHES Function

SELECT regexp_matches('aaa-bbb-ccc', '\w+', 'g');  -- 3 rows
FlagMeaning
gGlobal (all matches)
iCase-insensitive
nNewline-sensitive
xIgnore whitespace in pattern

4. Using REGEXP_REPLACE Function

SELECT regexp_replace('Phone: 555-1234', '\d', '*', 'g');

5. Using REGEXP_SPLIT_TO_TABLE Function

SELECT regexp_split_to_table('one,two;three', '[,;]') AS token;

6. Using REGEXP_SPLIT_TO_ARRAY Function

SELECT regexp_split_to_array('one,two;three', '[,;]');  -- {one,two,three}

7. Using Capture Groups

SELECT (regexp_match('John Doe, 42', '^(\w+) (\w+), (\d+)$'))[1] AS first,
       (regexp_match('John Doe, 42', '^(\w+) (\w+), (\d+)$'))[3] AS age;

8. Using Flags

FlagEffect
iCase-insensitive
gGlobal
s. matches newline
m^/$ per line

9. Using Character Classes

ClassMatches
\d / [[:digit:]]Digit
\w / [[:alnum:]_]Word char
\s / [[:space:]]Whitespace
[^...]Negated class

10. Using Anchors

AnchorMatch
^String start (or line with 'm')
$String end
\m / \MWord start / end
\yWord boundary

11. Using REGEXP_COUNT Function

SELECT regexp_count('aaa bbb aaa', 'aaa');   -- 2 (PG 15+)

12. Using REGEXP_INSTR Function

SELECT regexp_instr('foo bar baz', 'b\w+', 1, 2);   -- 9  (PG 15+)

13. Using REGEXP_SUBSTR Function

SELECT regexp_substr('id=42 type=admin', 'id=(\d+)', 1, 1, '', 1);   -- '42'
Note: regexp_count, regexp_instr, regexp_substr, regexp_like added in PG 15 for Oracle compatibility.