ruby - Parsing complex data to two dimensional array -
from sample info below,
"i: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\";i: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_sessiontime\";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:\"destination\";s:6:\"ausmob\";i:22;s:3:\"614\";s:6:\"prefix\";s:3:\"614\";i:23;s:6:\"ausmob\";"
i trying set value between i:\d*
, s:\d*
delimiters 2 dimensional array like:
[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.
what best way in ruby?
i believe works well:
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\""]]
ruby regex
No comments:
Post a Comment