3

I want to extract the first_name and second_name strings but with this regex only get the first_name string.

SELECT
   SUBSTRING (
      'a:2:{i:0;s:14:"first_name";i:1;s:15:"second_name";}',
      '\"[^\"]*\" '
   ) as obs_regex

How do I modify my regex to get first_name second_name as the result?

thanks you

2
  • I would use split_part() for that Commented Aug 27, 2019 at 7:19
  • Did my solution work for you? Commented Aug 28, 2019 at 6:49

1 Answer 1

5

You may match multiple occurrences using g flag and you need to use a capturing group to get the values without quoation marks:

SELECT
   REGEXP_MATCHES (
      'a:2:{i:0;s:14:"first_name";i:1;s:15:"second_name";}',
      '"([^"]*)"', 'g'
   ) as obs_regex

Result:

enter image description here

To get a concatenated string of matches you need to "convert" the regexp_matches result to an array and use array_to_string:

SELECT ARRAY_TO_STRING (
 ARRAY (
   SELECT REGEXP_MATCHES (
      'a:2:{i:0;s:14:"first_name";i:1;s:15:"second_name";}',
      '"([^"]*)"', 'g'
   )
 ), ' ') as obs_regex

Result:

enter image description here

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.