2014-10-20 55 views
1

我想構建一個正則表達式來評估一個字符串,該字符串在排序「ab」和「cd」時不包含兩個子字符串評估字符串的正則表達式,它不包含順序子字符串'ab'和'cd'

實施例:

"This is ab test for cd" <= Not matched because containing "ab", "cd" in order 
"This is cd test for ab" <= Matched because containing "ab" and "cd" but not in order 
"This is cd test" <= Matched because not containing ab 
"This is ab test" <= Matched because not containing cd 

回答

3

使用負前向斷言。

^(?!.*\sab\s.*?\scd\b).* 

OR

^(?!.*\bab\b.*?\bcd\b).* 

DEMO

+2

你很快+1 – anubhava 2014-10-20 10:03:24

相關問題