FastParse, search an expression in a free text

139 Views Asked by At

I'm trying FastParse library, but, I'm not sure then is the correct library for what I want to do.

In my test, I'm looking for a 'data' put in the middle of text paragraph, the text is this:

INTEL SSD 180 GB Serie 540s Interfaccia Sata III 6 Gb / s 2.5"

I'm trying to capture the value "180 GB", but, after different intents, I'm not sure if it's possible.

A bit of code:

lazy val spaceSep = "\t" | " " | "\r" | "\n" | "\u000C"
val digits = P(CharIn('0' to '9').rep(1).!).map(_.toInt)
lazy val GBSymbol = P( IgnoreCase("gb") | IgnoreCase("gigabyte"))
lazy val GB = P( AnyChar.rep ~ digits.! ~ spaceSep.rep ~ GBSymbol)

testFastParse.GB.parse("INTEL SSD 180 GB Serie 540s Interfaccia Sata III 6 Gb / s 2.5\"")

The last error "is scala.MatchError: Failure(CharIn("0123456789"):1:63 ..."") (of class fastparse.core.Parsed$Failure)"

Can anyone help me? thank you in advance

1

There are 1 best solutions below

1
chengpohi On BEST ANSWER

AnyChar.rep will not work in here since when start with AnyChar.rep, it's not possible to backtrack. if it's always start with the ALPHA maybe you can do it like:

  val spaceSep = P("\t" | " " | "\r" | "\n" | "\u000C")
  val digits: P[Int] = P(CharIn('0' to '9').rep(1).!).map(_.toInt)
  val GBSymbol = P(IgnoreCase("gb") | IgnoreCase("gigabyte"))
  val desc = P((CharIn('A' to 'Z') | CharIn('a' to 'z')).rep)
  val GB: P[Int] = P(desc.rep(sep = spaceSep) ~ digits ~ spaceSep.? ~ GBSymbol ~ AnyChar.rep)
  GB.parse("INTEL SSD 180 gigabyte  Serie 540s Interfaccia Sata III 6 Gb / s 2.5") match {
    case Parsed.Success(value, _) => println(value)
    case Parsed.Failure(_, _, detail) => println(detail)
  }

and also need to call out digits.! is unnecessary in there, since it's already captured by digits parser.