2017-09-14 62 views
0

我想有一个正则表达式模式匹配的行:
1,本行必须包含字符串的末尾不能“单词“S200”
2.消费满”,‘爵士’,‘JSON’,‘CSS’正则表达式来排除模式,但包括图案

这里是一个怪物我,不工作

(?=^.*$(?<!sping)(?<!js)(?<!css)(?<!json))(?=s200)

我是新来的正则表达式,任何帮助,将aapreciated !

+0

这_must_是正则表达式? –

+0

'(?s)^(?!sping $)(?!js $)(?! css $)(?! json $)(?= s200)' –

回答

1

对于初学者来说,你的正则表达式不匹配任何东西,因为你只有你的正则表达式。

?=   # look ahead for match 
?<!   # negative look behind 

换句话说,你不匹配任何东西,你的正则表达式,你正在寻找一个字符串position

解释:

(?=    # pos. lookahead 
^.*$   # read anything 
       # and AFTER reading everything, check 
(?<!sping)  # if you have NOT read sping 
(?<!js)   # if you have NOT read js 
(?<!css)  # if you have NOT read css 
(?<!json)  # if you have NOT read json 
) 
(?=s200)   # from this position, check if there's "s200" ahead. 

结论:你的正则表达式永远不会满足您的要求。

你可以只用一个正则表达式解决这个问题,例如使用:

(.*)s200(.*)$(?<!css|js|json|sping) 

它说

.*      # read anything 
s200      # read s200 
.*      # read anything 
$      # match the end of the string 
(?<!css|js|json|sping) # negative lookbehind: 
         # if you have read css,js,json or sping, fail 

你可以在两个步骤做到这一点很简单:

  • 第一次检查如果字符串包含s200与/s200/
  • 检查是否字符串不以消费满,JS,JSON或JSON结束与/css|js(on)?|sping$/
1

您已经标记这是perl,所以这里是一个perl解决方案:

$_ = $stringToTest; 
if (/s200/) { 
    # We now know that the string contains "s200" 
    if (/sping|json|js|css$/) { 
     # We now know it end with one of sping,json,js or css 
    } 
} 
+0

您应该解释为什么OP的工作不起作用,因为这是个问题。 –