【问题标题】:Postgresql replace all occurrences of string+Postgresql 替换所有出现的字符串+
【发布时间】:2019-01-10 13:35:17
【问题描述】:

我有这个字符串:

this is the abcd xxx
string I want to abcd yyy
replace in my text abcd zzz

现在我想用空白替换 abcd 及其后面的任何内容。

我想要这个结果:

this is the
string I want to 
replace in my text 

我试过了:

select regexp_replace(str, 'abcd.*','','gi')

但它只是在第一场比赛后删除了所有内容。还有其他没有运气的组合。

我错过了什么?

谢谢!

【问题讨论】:

    标签: regex postgresql regexp-replace


    【解决方案1】:

    在regexp_replace() 中使用标志n(换行敏感匹配):

    with my_table(str) as (
    values(
    'this is the abcd xxx
    string I want to abcd yyy
    replace in my text abcd zzz')
    )
    
    select regexp_replace(str, 'abcd.*','','gin')
    from my_table
    
       regexp_replace    
    -----------------
     this is the        +
     string I want to   +
     replace in my text 
    (1 row)
    

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 2014-08-05
      • 2012-08-01
      • 2011-02-23
      • 1970-01-01
      • 2017-06-21
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多