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"     } } 

if want flexible, need:

((?:\d+\s*[-+]\s+)*\d+) 

with live demo


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -