0

I have a string in PostgreSQL 11:

'for values from ('2022-01-01') to ('2023-01-01')'

And I need to extract all values between brackets:

expected result: '2022-01-01', '2023-01-01'

If I use the following construction, I'm able to extract only the first value:

select substring('FOR VALUES FROM (''2020-01-01'') TO (''2021-01-01'')' from '\((.*?)\)');

Is it possible to extract the second value? Or both values as one array?

2 Answers 2

1

You can use regexp_match():

select regexp_matches('for values from (''2022-01-01'') to (''2023-01-01'')', '\((.[^)]+)\)', 'g')
Sign up to request clarification or add additional context in comments.

Comments

0

If there's only one level of parenthesis than you can use regexp_matches() with the g (global) flag.

SELECT *
       FROM regexp_matches('for values from (''2022-01-01'') to (''2023-01-01'')', '\([^\)]*\)', 'g');

Otherwise, if there are more levels, that'll get messy results though. In such a case you need to define which is the level you're after and regular expressions might be of limited use in such a case.

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.