Scala regex works great under either of two conditions:
unconditionally executed code:
e.g.
val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
val anotherregx = """([\w]+)\t([/\w+=\-!%# ]+)""".r
val lineregx(category, aaUrl, title)
or if inside a case statement we consume the expressions (and don’t need them again..)
val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
line match {
case lineregx(category, aaUrl, title) => // do something with category, aaUrl and title in here!
case anotherRegx(category, aaUrl) => // do something different with category, aaUrl and title in here!
case _ => { println("did not match line %s".format(line)); return 0 }
}
But what about if i need to ‘surface’ the matches to variables outside of the case statement? Specifically the var’s shown below,
val lineregx = """([\w]+)\t([/\w.+-]+)\t([/\w+=\-!%# ]+)""".r
var category = "" ; var aaUrl = "";var title = ""
line match {
case lineregx(category, aaUrl, title) => val lineregx(category, aaUrl, title) = line
case anotherRegx(category, aaUrl) => val lineregx(category, aaUrl) = line
case _ => { println("did not match line %s".format(line)); return 0 }
}
// Do something with category, aaUrl, title HERE after the case statement.
Problem is , the syntax for applying the lineregx/anotherregex makes those variables local to the case statement only.
Roughly,
But that code is quite disorganized. For one thing, there’s the question of the value of
titlefor the second case. For another, there’s the early return. Instead, the code would probably be best organized like this: