regex - What's the best way to match strings in a file to case class in Scala? -
we have file contains data want match case class. know enough brute force looking idiomatic way in scala.
given file:
#record name:john doe age: 34 #record name: smith holy age: 33 # comment #record # comment name: martin fowler age: 99 (field values on 2 lines invalid, e.g. name:john\n smith should error)
and case class
case class record(name:string, age:int) i want return seq type such stream:
val records: stream records the couple of ideas i'm working far haven't implemented is:
remove new lines , treat whole file 1 long string. grep match on string "((?!name).)+((?!age).)+age:([\s\d]+)" , create new object of case class each match far regex foo low , can't match around comments.
recursive idea: iterate through each line find first line matches record, recursively call function match name, age. tail recursively return
some(new record(cumulativemap.get(name), cumulativemap.get(age))ornonewhen hitting nextrecordaftername(i.e.agenever encountered)?? better idea?
thanks reading! file more complicated above rules equal. curious: i'm trying parse custom m3u playlist file format.
you use parser combinators.
if have file format specification in bnf or can write one, scala can create parser rules. may more robust hand-made regex based parsers. it's more "scala".
Comments
Post a Comment