You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
REGEXP_MATCHES(str, pattern): a powerful function in PostgreSQL that matches regular expression patterns in strings and returns all matched results
Use case
Matching a single pattern:
Let's say we have a string from which we want to extract substrings that match a particular pattern
REGEXP_MATCHES('The quick brown fox', 'quick')--returns {quick}
Extract multiple subpatterns
Multiple substrings can be extracted using capture groups in regular expressions
REGEXP_MATCHES('User: Alice, ID: 12345', 'User: (\w+), ID: (\d+)')--returns {Alice,12345}
Globally match multiple patterns
The g flag can be used when you want to match all the substrings that occur
REGEXP_MATCHES('foo 123 bar 456', '\d+', 'g')--returns {"123","456"}
Case matching is ignored
Use the i flag to ignore case for matching
REGEXP_MATCHES('Hello World', 'hello', 'i')--returns {Hello}
Multi-line pattern matching
Using the m flag allows regular expressions to match across multiple lines
REGEXP_MATCHES('First line\nSecond line', '^Second', 'm')--returns {Second}
Some Announcements:
REGEXP_MATCHES() returns an array, so be careful about using arrays when processing the results.
If the global flag g is not used, only the first matching substring is returned.
Description
parent: #10796
Use case
Let's say we have a string from which we want to extract substrings that match a particular pattern
REGEXP_MATCHES('The quick brown fox', 'quick')--returns {quick}
Multiple substrings can be extracted using capture groups in regular expressions
REGEXP_MATCHES('User: Alice, ID: 12345', 'User: (\w+), ID: (\d+)')--returns {Alice,12345}
The g flag can be used when you want to match all the substrings that occur
REGEXP_MATCHES('foo 123 bar 456', '\d+', 'g')--returns {"123","456"}
Use the i flag to ignore case for matching
REGEXP_MATCHES('Hello World', 'hello', 'i')--returns {Hello}
Using the m flag allows regular expressions to match across multiple lines
REGEXP_MATCHES('First line\nSecond line', '^Second', 'm')--returns {Second}
REGEXP_MATCHES() returns an array, so be careful about using arrays when processing the results.
If the global flag g is not used, only the first matching substring is returned.
Are you willing to submit PR?
Code of Conduct
The text was updated successfully, but these errors were encountered: