A text file should be parsed line by line, using Scala pattern matching and regular expressions. If a line starts with "names:\t" the subsequent tab-separated names should be provided as a Seq[String] (or something similar).
Here a non-working code example:
val Names = "^names:(?:\t([a-zA-Z0-9_]+))+$".r
"names:\taaa\tbbb\tccc" match {
case Names(names @ _*) => println(names)
// […] other cases
case _ => println("no match")
}
Output: List(ccc)
Wanted output: List(aaa, bbb, ccc)
The following code works as desired…
object NamesObject {
private val NamesLine = "^names:\t([a-zA-Z0-9_]+(?:\t[a-zA-Z0-9_]+)*)$".r
def unapplySeq(s: String): Option[Seq[String]] = s match {
case NamesLine(nameString) => Some(nameString.split("\t"))
case _ => None
}
}
"names:\taaa\tbbb\tccc" match {
case NamesObject(names @ _*) => println(names)
// […] other cases
case _ => println("no match")
}
Output (as wanted): WrappedArray(aaa, bbb, ccc)
I would like to know: Is this is possible in a simpler manner without creating an object, just like in the first but non-working code example?
RegExextractor is a match, not a find, so the^and$are redundant. If you want find semantics, you need.*at the beginning and / or end. (Not relevant to the problem you're trying to solve, though.)