Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL substring get string between brackets

I have a string, say:

Product Description [White]

I want to extract anything inside the brackets (in this case White) from that string, using the PostgreSQL Substring function. I can get this to work using regexp_matches, but that returns an array which I don't want unless I have no other choice.

I've tried:

  • substring('string' from '[(.)]') >>> NULL
  • substring('string' from '\[(.)\]') >>> NULL
  • substring('string' from '\\[(.)\\]') >>> NULL

But this works:

  • substring('string' from 'W(.)i]') >>> h

What am I doing wrong?

like image 598
gnarlybracket Avatar asked Dec 17 '15 15:12

gnarlybracket


1 Answers

(.) only matches a single character, but you want to match multiple characters in there.

So you need (.+)

substring('Product Description [White]' from '\[(.+)\]')
like image 96
a_horse_with_no_name Avatar answered Sep 20 '22 17:09

a_horse_with_no_name