Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regexp_matches better way to get rid of returning curly brackets

Tags:

postgresql

Is there some better way to trim {""} in result of regexp_matches than:

trim(trailing '"}' from trim(leading '{"' from regexp_matches(note, '[0-9a-z \r\n]+', 'i')::text))
like image 258
Oleg Tsymbalyuk Avatar asked Nov 08 '12 10:11

Oleg Tsymbalyuk


1 Answers

regexp_matches() returns an array of all matches. The string representation of an array contains the curly braces that's why you get them.

If you just want a list of all matched items, you can use array_to_string() to convert the result into a "simple" text data type:

array_to_string(regexp_matches(note, '[0-9a-z \r\n]+', 'i'), ';')

If you are only interested in the first match, you can select the first element of the array:

(regexp_matches(note, '[0-9a-z \r\n]+', 'i'))[1]
like image 195
a_horse_with_no_name Avatar answered Sep 27 '22 21:09

a_horse_with_no_name