9951 explained code solutions for 126 technologies


postgresqlHow do I use PostgreSQL regexp to match a pattern?


PostgreSQL provides the regexp_matches function to match a pattern. This function returns all matched parts of a string.

Example

SELECT regexp_matches('Hello World', '\w+ \w+', 'g');

Output example

{Hello,World}

The code above uses regexp_matches to search for two words in a string.

Parts of the code:

  • SELECT regexp_matches - this is the function used to match a pattern
  • ('Hello World', '\w+ \w+', 'g') - this is the pattern to be matched. \w+ \w+ means one or more word characters followed by a space followed by one or more word characters. The g flag stands for global and means that the pattern should be matched multiple times.

Helpful links

Edit this code on GitHub