-1

In PostgreSQL, I have a text value foo (text1) bar (text2), I want to regex match and extract all the texts between parentheses

SELECT REGEXP_MATCHES('foo (text1) bar (text2)', '\((.*)\)', 'g')

I got this response:

regex_matches
-------------------
{text1) bar (text2}

I expect the output to be:

regex_matches
-------------------
text1
text2
GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39
Kevin Lee
  • 401
  • 3
  • 9
  • 22

1 Answers1

0

Try with this regex: \(([^\(]*)\)

Query:

SELECT REGEXP_MATCHES('foo (text1) bar (text2)', '\(([^\(]*)\)', 'g');

regex_matches return matches with curly brackets. If you want remove curly brackes ( { ).. then try with this below query:

SELECT ARRAY_TO_STRING( REGEXP_MATCHES('foo (text1) bar (text2)', '\(([^\(]*)\)', 'g'), '');
GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39
  • This answer is good, but it will return also empty value in case you have this in your string foo (text1) bar (text2) (). To avoid that, try this `\(([^\(]+)\)` – Abel LIFAEFI MBULA May 03 '20 at 10:25