4

I have a scenario in postgres where I need to gather all entries between multiple sets of square brackets.

The following example would be what I would expect to capture this:

SELECT (regexp_matches('Hello [World] How [Are] You','\[(.*?)\]')) 

But this simply returns

{World}

ignoring the second [Are] section.

In a regular regex this seems to work, so I'm unsure as to why its failing here.

Ideally, I would like to return the result as as csv text string. e.g.

World,Are

but I can't seem to find the right query to do this.

Any input appreciated. Thanks.

1 Answer 1

3

You have to use the 'g' flag

SELECT (regexp_matches('Hello [World] How [Are] You','\[(.*?)\]','g'))

The "g" flag indicates that the regular expression should be tested against all possible matches in a string.

Sign up to request clarification or add additional context in comments.

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.