15

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?

2 Answers 2

26

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

So you need (.+)

substring('Product Description [White]' from '\[(.+)\]')
Sign up to request clarification or add additional context in comments.

5 Comments

Only missing one character... Thanks a lot!
your response helped me a lot. Thanks!!
How can you do this for all matches in brackets
This doesn't work with multiple brackets. "Product Description [White][Black] would return White][Black... looking for a way to improve this to fit my needs.
WHAT IF? substring('Product Description [White] [Blue]' from '[(.+)]')
-2

i think you want to update some value like grade pay from pay scale or any thing just use following query

UPDATE master.pay_scale SET grade_pay= Case When description LIKE '%(%)%' Then (select substring(description from '((.+))')) else description End::numeric

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.