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?

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.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.