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!
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 ([%/])".rwould matchdivOp(op), but notdivOp().