【发布时间】:2018-10-25 08:48:58
【问题描述】:
假设我有一个字符串:
{"id":"35","value":"0.2"},{"id":"1462","value":"0.2"},
{"id":"1109","value":"0.2"},{"id":"220","value":"0.2"},
{"id":"211","value":"0.1"}
我需要提取每个{}中的子字符串
比在标题中创建具有 id 和与 id 对应的数字的列,例如:
35 1462 1109 220 211
----------
0.2 0.2 0.2 0.2 0.1
【问题讨论】:
-
简单,使用 JSON 解析器,然后遍历您的 JSON 内容以生成您想要的输出。这不是
sub或任何其他 R 正则表达式函数的工作。 -
s <- '{"id":"35","value":"0.2"},{"id":"1462","value":"0.2"}, {"id":"1109","value":"0.2"},{"id":"220","value":"0.2"}, {"id":"211","value":"0.1"}'; L <- readLines(textConnection(gsub("\\},", "}\n", s))); L <- L[L != ""]; ndjson::flatten(L)
标签: r substring text-extraction