我想检查一个字符串是否不包含字符串,例如:
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."
我需要检查哪个字符串不包含not
和also
。因此,预期结果为:str1
为false
,str2
为false
,str3
为true
。
什么是regexp?
答案 0 :(得分:2)
您可以使用一系列预测:
(?=.*not)(?=.*also).*
答案 1 :(得分: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)
答案 2 :(得分:0)
你也可以不用前瞻来解决这个问题。使用 x 修饰符可以更容易理解正则表达式。
/.*? \b not \b .*? \b also \b | .*? \b also \b .* \b not \b/x