【问题标题】:postgres substring regex multiple resultspostgres子字符串正则表达式多个结果
【发布时间】:2018-04-02 23:21:29
【问题描述】:

我正在尝试在具有多个实例的文本字段中查找图像标签 url。

我目前正在使用此代码从文本字段中提取 URL:

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

问题是它只返回图像标签的第一个实例。

有没有办法从单个查询中返回多个匹配实例?

【问题讨论】:

    标签: regex postgresql pattern-matching substring


    【解决方案1】:

    使用带有'g'标志的函数regexp_matches(),例如:

    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)
    

    在文档中了解POSIX Regular Expressions

    【讨论】:

      猜你喜欢
      • 2020-03-05
      • 2015-04-14
      • 2014-10-11
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      相关资源
      最近更新 更多