如何否定 Groovy 匹配运算符?
How to negate the Groovy Match Operator?
documentation 提到了三个特定于正则表达式的运算符:
~
返回一个 Pattern
=~
返回 Matcher
==~
返回一个 boolean
现在,我如何否定最后一个? (我同意其他人不能有任何有意义的否定。)
我尝试了明显的思路:
println 'ab' ==~ /^a.*/ // true: yay, matches, let's change the input
println 'bb' ==~ /^a.*/ // false: of course it doesn't match, let's negate the operator
println 'bb' !=~ /^a.*/ // true: yay, doesn't match, let change the input again
println 'ab' !=~ /^a.*/ // true: ... ???
我想最后两个应该这样解释:
println 'abc' != ~/^b.*/
我在哪里可以看到 new String("abc") != new Pattern("^b.*")
是 true
。
据我所知,Groovy.
中没有取反的正则表达式匹配运算符
所以 - 正如 cfrick 已经提到的 - 似乎最好的答案是否定整个表达式:
println !('bb' ==~ /^a.*/)
另一个解决方案是反转正则表达式,但在我看来可读性较差:
How can I invert a regular expression in JavaScript?
documentation 提到了三个特定于正则表达式的运算符:
~
返回一个Pattern
=~
返回Matcher
==~
返回一个boolean
现在,我如何否定最后一个? (我同意其他人不能有任何有意义的否定。)
我尝试了明显的思路:
println 'ab' ==~ /^a.*/ // true: yay, matches, let's change the input
println 'bb' ==~ /^a.*/ // false: of course it doesn't match, let's negate the operator
println 'bb' !=~ /^a.*/ // true: yay, doesn't match, let change the input again
println 'ab' !=~ /^a.*/ // true: ... ???
我想最后两个应该这样解释:
println 'abc' != ~/^b.*/
我在哪里可以看到 new String("abc") != new Pattern("^b.*")
是 true
。
据我所知,Groovy.
中没有取反的正则表达式匹配运算符所以 - 正如 cfrick 已经提到的 - 似乎最好的答案是否定整个表达式:
println !('bb' ==~ /^a.*/)
另一个解决方案是反转正则表达式,但在我看来可读性较差:
How can I invert a regular expression in JavaScript?