Calculate mathematical operation stored in String using regex in Scala -
i want evaluate mathematical expression stored in string , print result.
i have use pattern matching in scala. wrote code below, not work, prints false
instead of 2
.
any appreciated.
object patternmatcher{ val s = "13 - 5 - 6" val pattern = "((\\d+\\s[+-]\\s){1,10}(\\d+){0,1})".r def main(args: array[string]) { println(matcher(s)) } def matcher(choice: string): = choice match { case pattern(choice) => choice case _ => "false" } }
Comments
Post a Comment