【问题标题】:RegEx Matching a Pattern Only after The First Pattern Was MatchedRegEx 仅在匹配第一个模式后才匹配模式
【发布时间】:2020-02-09 04:35:08
【问题描述】:

我试图仅从字符串返回匹配项,如果另一个模式首先在 Oracle 中使用 RegEx_LikeRegExp_Substr 匹配,但我得到的结果好坏参半。

以下是我拥有的数据示例以及我试图从中返回的数据:

+------------------+----------+ |数据 |目标 | +------------------+----------+ | 201301-P706466 肯尼迪经典 | P706466 | | 201301-706466 世界资源研究所经典 | 706466 | | 201301 706466 肯尼迪经典 | 706466 | | 201301 P706466 WRI 经典 | 706466 | | 201910-MLB-CVG4 |空 | | 201209-肯尼迪经典 |空 | | 201203-WRI 经典 |空 | | 201209 肯尼迪经典 |空 | | 201203 世界资源研究所经典 |空 | +------------------+----------+

查询:

select    pid.location
        , pnm.pn as mpn
        , pid.pn as pn
        , pid.batch
        , pid.goods_rcvd_batch as grb
        , pid.sn
        , pid.bin
        , Case When RegExp_Like(pid.bin, ''''^\d{6}\-|^\d{6}\s'''') Then RegExp_Substr(pid.bin, ''''\d{6}|(P)\d{6}.*?(\d{6})|((P)\d{6})'''') 
          End As Bin_Esn                        
from    pn_inventory_detail pid
join    pn_interchangeable pni on pid.pn = pni.pn_interchangeable
join    pn_master pnm on pni.pn = pnm.pn
where   pid.installed_ac is null
    and pid.owner is null
    and pid.loan_category is null
    and pid.nha_pn is null

我尝试了 this 的不同变体,但在 regex101 上,但正如您在导航到链接后所看到的那样,它比我需要的匹配更多。

我想我可能会一起走错路,这就是我在这里的原因。话虽如此,哪种模式适用于我的情况,并且最有效?

【问题讨论】:

    标签: regex oracle plsql


    【解决方案1】:

    您可以使用此正则表达式来达到问题中提到的目标列:

    ^\d{6}[-\s]{1}(\d{6}|P\d{6})
    

    演示: Here

    【讨论】:

    • 很接近,但没有雪茄。将其修改为以下工作:^\d{6}[-\s]{1}(\d{6}|(P)\d{6})。见:regex101.com/r/68ypex/1
    • 我无法通过您的数据判断开头是否只有字母 P,因为它没有提到,所以我试图消除三个字母的长度。
    猜你喜欢
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    相关资源
    最近更新 更多