Regex and exlude matches with "this" string

时间:2015-09-01 21:15:01

标签: regex

I am trying to generate a regex string that I can't seem to get right.

I want to match any lines containing -AS- AND -CO while excluding -DC-

CCO-AS-02-CO
EKY-2F-AS-02-CO
LKS-AS-06-CO
LKS-AS-DC-07
LKS-AS-52-CR
ATX-A2-DC-AS-08-CO
BAL-0F-AS-06-CO
BAL-2F-AS-03-CO
CNC-1F-AS-05-CO

I'm not familiar with how to perform and but know that the this works for the containing chunks: (.*AS.*CO)

Help?

3 个答案:

答案 0 :(得分:2)

^(?!.*-DC-).*-AS-.*-CO.*$

You can use lookahead .See demo.

https://regex101.com/r/sS2dM8/32

答案 1 :(得分:1)

你可以用纯粹的前瞻来做这件事:

^(?=.*-AS-)(?=.*-CO)(?!.*-DC-).*$

                              ^^ a line with:
                       ^^^ do not have -DC- in the line
               ^^^ do have -CO
   ^^^ do have -AS-
^ Anchor, start of line

Demo

这三个术语可以在线上以任何顺序排列。

答案 2 :(得分:0)

If the AS and CO could be out of order, this could detect their presence.

^(?!.*-DC(?=-|$))(?=.*-AS(?=-|$))(?=.*-CO(?=-|$)).+$