4

I'm trying to find image tags urls in a text field with multiple instances.

I'm currently using this code to extract the URL from the text field:

SUBSTRING(text_field FROM 'src="([^"]*).*')

The problem is it only returns the first instance of a image tag.

Is there a way to return multiple instances of matching from a single query?

1 Answer 1

6

Use the function regexp_matches() with the 'g' flag, example:

with my_table(text_field) as (
    values ('src="first";src="second"')
)

select match[1] as result
from my_table
cross join lateral regexp_matches(text_field, 'src="([^"]*)', 'g') as match

 result 
--------
 first
 second
(2 rows)

Read about POSIX Regular Expressions in the documentation.

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.