1

我想检查一个字符串是否不包含字符串,例如:

str1 = "He is a minion, he's so funny."
str2 = "He is not a minion, he's funny."
str3 = "He is not a minion, he's also funny."

我需要检查哪个字符串不包含notand also。所以,预期的结果是:str1is false, str2is false, str3is true

什么是正则表达式?

4

3 回答 3

2

您可以使用一系列前瞻:

(?=.*not)(?=.*also).*
于 2013-09-12T16:28:44.543 回答
0

您也可以在没有前瞻的情况下解决此问题。使用x修饰符使正则表达式更易于理解。

/.*? \b not \b .*? \b also \b | .*? \b also \b .* \b not \b/x 
于 2013-09-12T17:51:31.123 回答
0

要查找包含多个单词的行,请向前看

(?=.*\bnot\b)(?=.*\balso\b).*

正则表达式:

(?=           look ahead to see if there is:
 .*          any character except \n (0 or more times)
 \b           the boundary between a word char and something that is not a word char
  not         'not'
 \b           the boundary between a word char and something that is not a word char
)             end of look-ahead
(?=           look ahead to see if there is:
 .*          any character except \n (0 or more times)
 \b           the boundary between a word char and something that is not a word char
  also         'also'
 \b           the boundary between a word char and something that is not a word char
)             end of look-ahead
.*            any character except \n (0 or more times)

现场演示

于 2013-09-12T17:22:52.253 回答