1

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

4

1 回答 1

2

AnyChar.rep在这里不起作用,因为从 开始AnyChar.rep,不可能回溯。如果它总是以 the 开头,ALPHA也许你可以这样做:

  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)
  }

并且还需要调用digits.!在那里是不必要的,因为它已经被digits解析器捕获。

于 2017-08-24T14:55:41.597 回答