数学符号的 Scala 字符串模式匹配
我有以下代码:
val z: String = tree.symbol.toString
z match {
case "method +" | "method -" | "method *" | "method ==" =>
println("no special op")
false
case "method /" | "method %" =>
println("we have the special div operation")
true
case _ =>
false
}
Is it possible to create a match for the Primitive Operations in Scala:
"method *".matches("(method) (+-*==)")
我知道 (+-*) 符号用作量词。有办法匹配它们吗? 来自热心 Scala 学者的感谢!
I have the following code:
val z: String = tree.symbol.toString
z match {
case "method +" | "method -" | "method *" | "method ==" =>
println("no special op")
false
case "method /" | "method %" =>
println("we have the special div operation")
true
case _ =>
false
}
Is it possible to create a match for the primitive operations in Scala:
"method *".matches("(method) (+-*==)")
I know that the (+-*) signs are used as quantifiers. Is there a way to match them anyway?
Thanks from a avidly Scala scholar!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然。
需要考虑的事项:
[abc]
而不是(?:a|b|c)
来匹配单个字符。[]
时,-
必须是第一个字符,否则它将被解释为范围。同样,^
不能是[]
中的第一个字符,否则会被解释为否定。(?:...)
而不是(...)
因为我不想提取内容。例如,如果我确实想要提取内容——这样我就知道运算符是什么,那么我会使用(...)
。但是,我还必须更改匹配以接收提取的内容,否则匹配会失败。()
——例如divOp()
。如果您忘记了它们,则会进行简单的分配(Scala 会抱怨无法访问代码)。"method ([%/])".r
将匹配divOp(op)
,但不匹配divOp()
。Sure.
Things to consider:
[abc]
instead of(?:a|b|c)
.-
has to be the first character when using[]
, or it will be interpreted as a range. Likewise,^
cannot be the first character inside[]
, or it will be interpreted as negation.(?:...)
instead of(...)
because I don't want to extract the contents. If I did want to extract the contents -- so I'd know what was the operator, for instance, then I'd use(...)
. However, I'd also have to change the matching to receive the extracted content, or it would fail the match.()
on the matches -- likedivOp()
. If you forget them, a simple assignment is made (and Scala will complain about unreachable code)."method ([%/])".r
would matchdivOp(op)
, but notdivOp()
.与 Java 中的非常相似。要转义正则表达式中的字符,请在该字符前加上
\
前缀。然而,反斜杠也是标准 Java/Scala 字符串中的转义字符,因此要将其传递给正则表达式处理,您必须再次在 it 前面加上反斜杠。你最终会得到类似的结果:正如 James Iry 在下面的评论中指出的那样,Scala 还支持“原始字符串”,用三个引号括起来:
"""Raw string in which I don't need escape像 \!"""
这样可以避免 Java/Scala 字符串强加的第二级转义。请注意,您仍然需要转义正则表达式解析器视为特殊的任何字符:Much the same as in Java. To escape a character in a regular expression, you prefix the character with
\
. However, backslash is also the escape character in standard Java/Scala strings, so to pass it through to the regular expression processing you must again prefix it with a backslash. You end up with something like:As James Iry points out in the comment below, Scala also has support for 'raw strings', enclosed in three quotation marks:
"""Raw string in which I don't need to escape things like \!"""
This allows you to avoid the second level of escaping, that imposed by Java/Scala strings. Note that you still need to escape any characters that are treated as special by the regular expression parser:字符串中的转义字符与 Java 中的转义字符类似。
如果您有需要大量转义的较大字符串,请考虑 Scala 的
"""
。例如
"""String 不需要转义任何内容 \n \d"""
如果您在正则表达式周围放置三个
"""
,则无需再转义任何内容。Escaping characters in Strings works like in Java.
If you have larger Strings which need a lot of escaping, consider Scala's
"""
.E. g.
"""String without needing to escape anything \n \d"""
If you put three
"""
around your regular expression you don't need to escape anything anymore.