For a first test with scala combinators, I am trying to get all words from a sentence, but I am just getting "None" from the following code :
import java.io.File
import scala.io.Source
import scala.util.parsing.combinator._
object PgnReader extends TagParser {
def parseFile(inputFile:File) = {
val pgnStream = Source.fromFile(inputFile)
val pgnStr = pgnStream.mkString
println(parseAll(tag, "Hello World !").getOrElse("None"))
pgnStream.close
}
}
trait TagParser extends RegexParsers {
val tag:Parser[String] = """[:alpha:]+""".r ^^ (_.toString)
}
I would like to get something like :
Hello
World
or even like :
List(Hello, World)
Am I on the right way with my code ?
I am using scala 2.11 and scala combinators
I think this might get you closer:
POSIX character classes have a different syntax in Scala (as inherited from Java). The
rep()
syntax allows for multiple occurrences (giving aList()
).That will still choke on the exclamation point, so you can augment your regex a little. I'd probably also go with the notion of "tag" and "tags" separately to make things clearer: