将两个正则表达式合并为一个

时间:2014-01-02 05:03:41

标签: perl

我需要结合这两个正则表达式 将^.*(Completed: BALI - Job).*$\b(BALI_)(INVOICES|CORRESPONDENCE|LATE_SCANNINGS)\b合并为一个,以便"Completed: BALI - Job"作为强制匹配,"BALI_INVOICES or BALI_CORRESPONDENCE or BALI_LATE_SCANNINGS"作为替代

2 个答案:

答案 0 :(得分:1)

扩展David-SkyMesh的答案,使第一部分成为强制性,三个备选方案的第二部分可选:

/ (                                                           # capture
    (?:Completed: BALI - Job)                                 # this
                                                              # AND optionally
                                                              # one of these
    (?:\bBALI_(?:INVOICES|CORRESPONDENCE|LATE_SCANNINGS)\b)?  
  )
/x                       # ignore whitespace & comments in regexp itself

答案 1 :(得分:1)

由于第二部分是可选的,因此附加?

^.*(Completed: BALI - Job).*(\b(BALI_)(INVOICES|CORRESPONDENCE|LATE_SCANNINGS)\b)?