java - 匹配模式和计算相等性之间的区别

标签 java regex groovy

在正则表达式(Groovy)中,“== ~”运算符与运算符类似 “==”,但匹配模式而不是“计算相等” 您能否通过一个小例子帮助我理解这两者之间的实际区别?

最佳答案

x==yx.equals(y)x.compareTo(y) 的缩写 - 请参阅 Groovy Operator Overloading (在java中这基本上是比较对象身份)。

运算符==~是专门设计的比较运算符,可帮助您对字符串上的模式进行完全匹配。

为操作符生成的代码(例如 disasm 代码 'xxx' ==~/xx?/)是:

ScriptBytecodeAdapter.matchRegex("xxx", "xx?")

来自Groovy Regular Espressions :

Since a Matcher coerces to a boolean by calling its find method, the =~ operator is consistent with the simple use of Perl's =~ operator, when it appears as a predicate (in 'if', 'while', etc.). The "stricter-looking" ==~ operator requires an exact match of the whole subject string. It returns a Boolean, not a Matcher.

关于java - 匹配模式和计算相等性之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27146943/

相关文章:

java - 为每个搜索查询设置不同的页面长度

regex - Ksh 脚本 : Run bash-like character substitute: ${@/. txt/}

grails - 无法排序hasMany关系

groovy - 如何在 Groovy/Gradle 中操作存档中的文件?

grails - 减少 Grails 项目构建时间的常用技术

java - 将数组元素添加到 2d ArrayList

java - 获取ArrayList索引

java - 查找双链表的中间节点数为偶数

php - 从 google maps api 结果中删除重复的地址

javascript - 如果没有匹配项,正则表达式返回 null