数学符号的 Scala 字符串模式匹配

发布于 2024-10-12 15:03:02 字数 511 浏览 1 评论 0原文

我有以下代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梅倚清风 2024-10-19 15:03:02

当然。

val z: String = tree.symbol.toString
val noSpecialOp = "method (?:[-+*]|==)".r
val divOp = "method [/%]".r
z match {
  case noSpecialOp() =>
    println("no special op")
    false
  case divOp() =>
    println("we have the special div operation")
    true
  case _ =>
    false
}

需要考虑的事项:

  • 我选择使用 [abc] 而不是 (?:a|b|c) 来匹配单个字符。
  • 请注意,使用 [] 时,- 必须是第一个字符,否则它将被解释为范围。同样,^不能是[]中的第一个字符,否则会被解释为否定。
  • 我使用 (?:...) 而不是 (...) 因为我不想提取内容。例如,如果我确实想要提取内容——这样我就知道运算符是什么,那么我会使用(...)。但是,我还必须更改匹配以接收提取的内容,否则匹配会失败。
  • 重要的是不要忘记匹配上的()——例如divOp()。如果您忘记了它们,则会进行简单的分配(Scala 会抱怨无法访问代码)。
  • 而且,正如我所说,如果您要提取某些内容,那么您需要在括号内添加一些内容。例如,"method ([%/])".r 将匹配 divOp(op),但不匹配 divOp()

Sure.

val z: String = tree.symbol.toString
val noSpecialOp = "method (?:[-+*]|==)".r
val divOp = "method [/%]".r
z match {
  case noSpecialOp() =>
    println("no special op")
    false
  case divOp() =>
    println("we have the special div operation")
    true
  case _ =>
    false
}

Things to consider:

  • I choose to match against single characters using [abc] instead of (?:a|b|c).
  • Note that - 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.
  • I'm using (?:...) 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.
  • It is important not to forget () on the matches -- like divOp(). If you forget them, a simple assignment is made (and Scala will complain about unreachable code).
  • And, as I said, if you are extracting something, then you need something inside those parenthesis. For instance, "method ([%/])".r would match divOp(op), but not divOp().
枯寂 2024-10-19 15:03:02

与 Java 中的非常相似。要转义正则表达式中的字符,请在该字符前加上 \ 前缀。然而,反斜杠也是标准 Java/Scala 字符串中的转义字符,因此要将其传递给正则表达式处理,您必须再次在 it 前面加上反斜杠。你最终会得到类似的结果:

scala> "+".matches("\\+")
res1 : Boolean = true

正如 James Iry 在下面的评论中指出的那样,Scala 还支持“原始字符串”,用三个引号括起来: """Raw string in which I don't need escape像 \!""" 这样可以避免 Java/Scala 字符串强加的第二级转义。请注意,您仍然需要转义正则表达式解析器视为特殊的任何字符:

scala> "+".matches("""\+""")
res1 : Boolean = true

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:

scala> "+".matches("\\+")
res1 : Boolean = true

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:

scala> "+".matches("""\+""")
res1 : Boolean = true
做个少女永远怀春 2024-10-19 15:03:02

字符串中的转义字符与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文