I have a string, say var str = "hello, world" and a List of Regex patterns
val patterns = List(new Regex("hello, (.*)", "substr"), new Regex("hi, (.*)", "substr"))
How can I match str against these patterns? Now instead of using List of patterns I'm doing the following:
val pattern1 = new Regex("hello, (.*)", "substr")
val pattern2 = new Regex("hi, (.*)", "substr")
var someVar = "something"
var someVar2 = "something else"
str match {
case pattern1(substr) => { someVar = substr; someVar2 = "someValue" }
case pattern2(substr) => { someVar = substr; someVar2 = "someOtherValue" }
}
Appendix:
I forgot to mention one important thing: in fact there are several lists of patterns. And someVar2 takes its value depending on the list from which the first pattern match occurred. It does not matter for me whether to use nested lists like List(List(new Regex(...), new Regex(...), ...), List(new Regex(...), new Regex(...), ...)) or to use separate val for each list of patterns like val patterns1 = List(new Regex(...), ...); val patterns2 = List(new Regex(...), ...).