如何否定Groovy Match运算符?

编程入门 行业动态 更新时间:2024-10-23 07:27:07
本文介绍了如何否定Groovy Match运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

The documentation mentions three regex specific operators:

  • 〜返回一个 Pattern
  • =〜重设 Matcher
  • ==〜返回一个 boolean
  • ~ returning a Pattern
  • =~ returing a Matcher
  • ==~ returning a boolean

现在,我该如何否定最后一个?(我同意其他人不能有任何有意义的否定.)

Now, how can I negate the last one? (I agree that the others can't have any meaningful negation.)

我尝试了显而易见的想法:

I tried the obvious thinking:

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: ... ???

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

I guess the last two should be interpreted like this rather:

println 'abc' != ~/^b.*/

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

推荐答案

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

AFAIK, there is no negated regular expression match operator in Groovy.

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

So - as already mentioned by cfrick - it seems that the best answer is to negate the whole expression:

println !('bb' ==~ /^a.*/)

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

Another solution is to invert the regular expression, but it seems to me to be less readable:

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

更多推荐

如何否定Groovy Match运算符?

本文发布于:2023-11-22 13:19:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617572.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符   Groovy   Match

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!