首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“标签:价值,标签:价值,.”

“标签:价值,标签:价值,.”
EN

Stack Overflow用户
提问于 2015-11-06 16:53:44
回答 1查看 76关注 0票数 0

我有一个字符串,它看起来像"label:value“项的逗号列表。

代码语言:javascript
复制
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位于中间时,我正试图使类正常工作:

代码语言:javascript
复制
package parsers

object ReturnStringParser {
  def findVal(fieldName: String, s: String) = {
    val rx = s"(?<=$fieldName:)"+"(.*)*[^,\\s]*"
    (rx.r)
      .findAllIn(s)
      .toList
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-06 17:29:13

这样做是可行的:

代码语言:javascript
复制
val rx = s"(?<=$fieldName:)"+"([^,]*)"

https://regex101.com/r/aC4vA3/1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33571746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档