-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
  • 7,738
  • 5
  • 20
  • 36
Kevin Lee
  • 182
  • 1
  • 5
  • 16

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
  • 7,738
  • 5
  • 20
  • 36
  • 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 `\(([^\(]+)\)` – bam May 03 '20 at 10:25