将复杂数据解析为二维数组

时间:2014-10-16 06:08:52

标签: ruby regex

从以下示例数据中

  

“我:0; S:3:\” 471 \ “; S:2:\” ID \ “; S:3:\” 471 \ “; I:1; S:24:\” SIP / 61123456999-0000004c \ “; S:9:\” 的sessionid \ “; S:24:\” SIP / 61123456999-0000004c \ “; I:2; S:13:\” 1413095709.76 \ “; S:8:\” UNIQUEID \ “; S:13:\” 1413095709.76 \ “; I:3; S:0:\” \ “; S:12:\” nasipaddress \ “; S:0:\” \“; I:4; s:19:\“2014-10-12 18:35:23 \”; s:9:\“starttime \”; s:19:\“2014-10-12 18:35:23 \”;我: 5; s:19:\“2014-10-12 18:35:23 \”; s:8:\“stoptime \”; s:19:\“2014-10-12 18:35:23 \”; I:6; S:1:\ “0 \”; S:11:\ “sessiontime \”; S:1:\ “0 \”; I:7; S:11:\ “61412123948 \”; S: 13:\ “calledstation \”; S:11:\ “61412123948 \”; I:8; S:2:\ “ - 0 \”; S:11:\ “sessionbill \”; S:2:\“ - 0 \ “; I:9; S:1:\” 1 \ “; S:14:\” id_tariffgroup \ “; S:1:\” 1 \ “; I:10; S:1:\” 6 \ “; S:13:\” id_tariffplan \ “; S:1:\” 6 \ “; I:11; S:5:\” 31525 \ “; S:11:\” id_ratecard \“; S:5: \ “31525 \”; I:12; S:1:\ “1 \”; S:8:\ “id_trunk \”; S:1:\ “1 \”; I:13; S:1:\” 0 \ “; S:6:\” sipiax \ “; S:1:\” 0 \ “; I:14; S:11:\” 61123456999 \ “; S:3:\” SRC \“; S: 11:\ “61123456999 \”; I:15; N; S:6:\ “id_did \”; N; I:16; S:7:\ “0.00000 \”; S:7:\ “buycost \”; S:7:\ “0.00000 \”; I:17; N; S:21:\ “id_card_package_offer \”; N; I:18; S:1:\ “0 \”; S:16:\“real_s essiontime \ “; S:1:\” 0 \ “; I:19; S:13:\” 0061412123948 \ “; S:4:\” DNID \ “; S:13:\” 0061412123948 \“; I: 20,S:1:\ “4 \”; S:16:\ “terminatecauseid \”; S:1:\ “4 \”; I:21; S:3:\ “614 \”; S:11: \ “目的地\”; S:6:\ “AUSMOB \”; I:22; S:3:\ “614 \”; S:6:\ “前缀\”; S:3:\ “614 \”; I:23; S:6:\ “AUSMOB \”;“

我试图将i:\d*s:\d*分隔符之间的值放入二维数组中,如:

[0]["471"]
[0]["id"]
[0]["471"]

[1]["SIP/61123456999-0000004c"]
[1]["sessionid"]
[1]["SIP/61123456999-0000004c"]

[2]["1413095709.76"]
[2]["uniqueid"]
[2]["1413095709.76"]

etc.

Ruby 中执行此操作的最佳方式是什么?

2 个答案:

答案 0 :(得分:1)

([^\\";]+)\\[^\\]*\\"([^\\";]+)\\[^\\]*\\"([^\\";]+)

试试这个。看看demo.Use g flag.Grab捕获或匹配。

http://regex101.com/r/dZ1vT6/39

答案 1 :(得分:1)

我相信这也有效:

str.gsub(/s:\d+:/,'').split(/i:\d+;/)[1..-1].map { |s| s.split(';') }
  #=> [["\"471\"", "\"id\"", "\"471\""],
  #    ["\"SIP/61123456999-0000004c\"", "\"sessionid\"",
  #     "\"SIP/61123456999-0000004c\""],
  #    ["\"1413095709.76\"", "\"uniqueid\"", "\"1413095709.76\""],
  #    ...
  #    ["\"614\"", "\"prefix\"", "\"614\""],
  #    ["\"AUSMOB\""]]