如何否定Groovy匹配运算符?

TWi*_*Rob 23 regex groovy

文档提到了三个正则表达式特定的运算符:

  • ~ 回来了 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: ... ???
Run Code Online (Sandbox Code Playgroud)

我想最后两个应该像这样解释:

println 'abc' != ~/^b.*/
Run Code Online (Sandbox Code Playgroud)

在哪里,我可以看到new String("abc") != new Pattern("^b.*")存在true.

rdm*_*ler 33

AFAIK,Groovy中没有否定正则表达式匹配运算符.

所以 - 正如cfrick已经提到的那样 - 似乎最好的答案是否定整个表达:

println !('bb' ==~ /^a.*/)
Run Code Online (Sandbox Code Playgroud)

另一个解决方案是反转正则表达式,但在我看来,可读性较差:

如何在JavaScript中反转正则表达式?