Scala regex match failing with scala.MatchError for \w and \d (word or digit) match -
i trying basic regex pattern matching. although syntax seems correct, it's failing when use \w
or \d
word , digit matching.
import scala.util.matching.regex object ex { def main(args:array[string]):unit = { val pattern = new regex("(\\w)\\s(\\d)"); val pattern(words,num) = "asas1 11" print(words+" "+num) } }
this error get:
exception in thread "main" scala.matcherror: asas1 11 (of class java.lang.string) @ com.cccu.semantic.ex$.main(ex.scala:8) @ com.cccu.semantic.ex.main(ex.scala)
note: using scala ide build of eclipse sdk, build id 4.4.1 scala 2.11.8 on windows machine.
\w
, \d
match single character, need add there +
modifier. throwing exception because can't match input against regular expression.
scala> val pattern = new regex("(\\w+)\\s(\\d+)"); val pattern(words,num) = "asas1 11" pattern: scala.util.matching.regex = (\w+)\s(\d+) words: string = asas1 num: string = 11
Comments
Post a Comment