I am working on scala pattern matching.
For the below program I am getting output.
val pattern = "([0-9]+) ([A-Za-z]+)".r
val pattern(count,fruit) ="100 bananas"
The output of the program is
count: String = 100 fruit: String = bananas
I modified the program to get deeper understanding, by adding one more numeric pattern into the pattern val object and one more object into the extraction pattern val.
The program is
val pattern = "([0-9]+) ([A-Za-z]+) ([0-9]+) ".r
val pattern(count, fruit, price) ="100 bananas 1002"
But the program is not compiling and it is throwing error. The error is -
scala.MatchError: 100 bananas 1002 (of class java.lang.String)
at #worksheet#.x$1$lzycompute(ExtractingStrings.sc0.tmp:6)
at #worksheet#.x$1(ExtractingStrings.sc0.tmp:6)
at #worksheet#.count$lzycompute(ExtractingStrings.sc0.tmp:6)
at #worksheet#.count(ExtractingStrings.sc0.tmp:6)
at #worksheet#.#worksheet#(ExtractingStrings.sc0.tmp:6)
Can anyone explain why it is throwing this error. Thanks in advance.