正则表达式匹配文件

时间:2017-06-24 00:18:28

标签: regex scala

我是正则表达式的新手! 我想从字符串中获取特定的JSON对象,该对象可能包含多个JSON对象。 我在文件中有类似的行,如下所示,

Mon Jan 20 00:00:21 - 0800 2014,

  

{ “CL”: “JS”, “上”: “AAA”, “IP”: “50.100.200.210”, “CC”: “US”, “RG”: “CA”, “CT”: “民航局”, “PC”: “94538”, “MC”:807, “BF”: “9d9b0900247ab80b9dc4c60013245fa36d8d4dc4”, “伟仕”: “2074ae4d-9969-4103-9637-92a31ba8a108”, “LT”:“星期一   1月20日00:00:14 -0800 2014“,”hk“:[”sub“,”hatch“]},   { “V”: “1.1”, “PV”: “cc4c68d2-54b3-4607-9578-619f68b97947”, “R”: “V3”, “T”: “f1951bc8”, “一个”:[{ “i” 的:2, “U”: “u.jpg”, “W”:800, “H”:532, “×”:198, “Y”:1655, “LT”: “无”, “AF”:假}], “RF”: “正则表达式”, “p”: “阶”, “FS”:真, “TR”:0.9, “AC”:{ “20722”:1}, “VP”:{“二“:假,” W “:1905,” H “:955},” SC “:{” W “:1920,” H “:1080,” d “:1},” PID “:7343,” VID” :8}

我想解析整个字符串并将JSON对象解压缩为JSON字符串,该字符串包含第三列JSON对象中的键“pv”。可能有一些行没有第一个JSON对象,但行中有时间列和第二个JSON对象。

我从另一个帖子中找到了\{([^{}]|)*\}这个匹配第一个JSON对象和第二个对象的部分表达式!我需要从总线中获得第二个JSON,其结构中有“pv”键!对给定表达式的任何建议只能得到第三列!!

2 个答案:

答案 0 :(得分:0)

您可以像这样使用liftweb json库:

import net.liftweb.json._
val p = net.liftweb.json.parse("""[{"cl":"js","up":"aaa","ip":"50.100.200.210","cc":"US","rg":"CA","ct":"CAA","pc":"94538","mc":807,"bf":"9d9b0900247ab80b9dc4c60013245fa36d8d4dc4","vst":"2074ae4d-9969-4103-9637-92a31ba8a108","lt":"Mon Jan 20 00:00:14 -0800 2014","hk":["sub","hatch"]}, {"v":"1.1","pv":"cc4c68d2-54b3-4607-9578-619f68b97947","r":"v3","t":"f1951bc8","a":[{"i":2,"u":"u.jpg","w":800,"h":532,"x":198,"y":1655,"lt":"none","af":false}],"rf":"regex","p":"scala","fs":true,"tr":0.9,"ac":{"20722":1},"vp":{"ii":false,"w":1905,"h":955},"sc":{"w":1920,"h":1080,"d":1},"pid":7343,"vid":8}]""")
compact(render(p \\ "pv"))
res3: String = "cc4c68d2-54b3-4607-9578-619f68b97947"

\\运算符执行搜索给定键。有关详细信息,请参阅此处:https://github.com/lift/lift/tree/master/framework/lift-base/lift-json

注意 - 我在json字符串的开头和结尾添加了方括号,以便它是有效的json ..

答案 1 :(得分:0)

你也可以像这样使用原生的scala json库:

val p2 = scala.util.parsing.json.JSON.parseFull("""[{"cl":"js","up":"aaa","ip":"50.100.200.210","cc":"US","rg":"CA","ct":"CAA","pc":"94538","mc":807,"bf":"9d9b0900247ab80b9dc4c60013245fa36d8d4dc4","vst":"2074ae4d-9969-4103-9637-92a31ba8a108","lt":"Mon Jan 20 00:00:14 -0800 2014","hk":["sub","hatch"]}, {"v":"1.1","pv":"cc4c68d2-54b3-4607-9578-619f68b97947","r":"v3","t":"f1951bc8","a":[{"i":2,"u":"u.jpg","w":800,"h":532,"x":198,"y":1655,"lt":"none","af":false}],"rf":"regex","p":"scala","fs":true,"tr":0.9,"ac":{"20722":1},"vp":{"ii":false,"w":1905,"h":955},"sc":{"w":1920,"h":1080,"d":1},"pid":7343,"vid":8}]""")

p2.flatMap { l => Some(l.asInstanceOf[List[Map[String,Any]]]) }.
  flatMap(l2 => { Some(l2.flatMap( m => {
    m.get("pv")
  }))
})

res37: Option[List[Any]] = Some(List(cc4c68d2-54b3-4607-9578-619f68b97947))