【问题标题】:RegEx in Oracle SQL, positive look aheadOracle SQL 中的正则表达式,积极展望未来
【发布时间】:2020-03-04 04:22:32
【问题描述】:

我有一个字符串,我想捕获 "\Close_Out" 之前的所有内容:

字符串:\fileshare\R and G\123456\Close_Out\Warranty Letter.pdf

我想出的唯一解决方案是使用正向前瞻,当我在 https://regex101.com/ 上测试它时有效

(.*)(?=\\Close_Out)

但现在我需要在 Oracle SQL 语句中使用它:

select REGEXP_SUBSTR('\\fileshare\R and G\123456\Close_Out\Warranty Letter.pdf', '(.*)(?=\\Close_Out)') from dual

它不起作用,因为(我认为)不支持向前看。有人可以协助使用可在 sql 中使用的替代表达式

【问题讨论】:

    标签: regex oracle


    【解决方案1】:

    如果正则表达式不是必须,那么substr + instr 就可以了:

    SQL> with test (col) as
      2    (select '\fileshare\R and G\123456\Close_Out\Warranty Letter.pdf' from dual)
      3  select substr(col, 1, instr(col,'\Close_Out') - 1) result
      4  from test;
    
    RESULT
    -------------------------
    \fileshare\R and G\123456
    
    SQL>
    

    【讨论】:

    • 谢谢!我试图使用 REGEX 使事情变得复杂。感谢您的解决方案!
    【解决方案2】:

    为了完整起见,此 REGEXP 提​​供了结果包含\Close_Out

    select REGEXP_SUBSTR('\\fileshare\R and G\123456\Close_Out\Warranty Letter.pdf', '.*\\Close_Out') reg from dual;
    
    
    REG                                 
    ------------------------------------
    \\fileshare\R and G\123456\Close_Out
    

    要获取字符串 before 它使用 subexpression - 括在括号中的部分并使用 subexpression 参数 = 1 引用它(最后参数 - 详见documentation)。

    select REGEXP_SUBSTR('\\fileshare\R and G\123456\Close_Out\Warranty Letter.pdf', '(.*)\\Close_Out', 1, 1, null, 1) reg from dual;
    
    
    REG                       
    --------------------------
    \\fileshare\R and G\123456
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2019-08-19
      • 2021-05-22
      相关资源
      最近更新 更多