我有一个字符串,看起来像逗号分隔的"标签:值"项目
package testParsers
import org.scalatest.{Matchers, FlatSpec}
class testReturnStrParser extends FlatSpec with Matchers{
import parsers.ReturnStringParser
"return string parser" should "find the height in ret string" in {
val teststr = "blahblah:123, height:80.3"
val s = ReturnStringParser.findVal("height", teststr)
s should have length 1
s.head shouldEqual ("80.3")
}
it should "work if it is in the middle" in {
val teststr = "blahblah:123, height:80.3,weight:100.0"
val s = ReturnStringParser.findVal("height", teststr)
s should have length 1
s.head shouldEqual ("80.3")
}
}
当标签height
位于中间时,我正在努力使课程正常工作:
package parsers
object ReturnStringParser {
def findVal(fieldName: String, s: String) = {
val rx = s"(?<=$fieldName:)"+"(.*)*[^,\\s]*"
(rx.r)
.findAllIn(s)
.toList
}
}