【问题标题】:How to extract text between two words in OracleOracle中如何提取两个单词之间的文本
【发布时间】:2020-05-05 21:25:11
【问题描述】:

这是源字符串:

random foobar "name" : "Jack Ryan", other random stuff

在 Oracle 查询中,如何从中提取 Jack Ryan?

我想我正在寻找介于 "name" : " 和 ", 之间的任何内容

【问题讨论】:

    标签: sql regex string oracle select


    【解决方案1】:

    一个选项是regexp_replace():

    regexp_replace(col, '.*"name" : "([^"]+)".*', '\1')
    

    你也可以使用regexp_substr():

    regexp_substr(col, '"name" : "([^"]+)"', 1, 1, null, 1)
    

    这将捕获字符串'"name" : ' 之后的双引号内的字符串部分。

    Demo on DB Fiddle:

    with t as (select 'random foobar "name" : "Jack Ryan", other random stuff' col from dual)
    select 
        col, 
        regexp_replace(col, '.*"name" : "([^"]+)".*', '\1') newcol1,
        regexp_substr(col, '"name" : "([^"]+)"', 1, 1, null, 1) newcol2
    from t
    
    色彩 |新科尔1 | NEWCOL2 :------------------------------------------------ ---- | :-------- | :-------- random foobar "name" : "Jack Ryan",其他随机的东西 |杰克瑞恩 |杰克瑞恩

    【讨论】:

    • 谢谢。一个后续问题 - 如果我需要找到此模式的第二个实例怎么办?
    • @Stubborn:你可以使用regexp_substr()并将第四个参数设置为2而不是1。
    猜你喜欢
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多