背景
相关问题,但不需要阅读
问题
我有一串
str_temp <- "{type: [{a: a1, timestamp: 1}, {a:a2, timestamp: 2}]}"
从中我要提取所有七个字母数字子字符串:type, a, a1, timestamp, a, a2, timestamp
。但是,我无法使我的正则表达式正常工作。
我已经尝试了基础R和library(stringr)
使用[:word:], [:alnum:], [:alpha:]
etc的各种组合。
一个例子:
> pattern <- "[:word:]"
> str_locate_all(str_temp, pattern)
[[1]]
start end
[1,] 6 6
[2,] 11 11
[3,] 26 26
[4,] 34 34
[5,] 48 48
但是这只是给我的终点弦type
,a
,timestamp
,a
,timestamp
而不是起点,或任一a1
或a2
。
提取所有七个字母数字字符串的正确正则表达式是什么?
这是一个有效的正则表达式。匹配所有字母数字词,但不匹配数字。
((?![0-9]+)[A-Za-z0-9]+)
http://www.rubular.com/r/EuF9AfdtXW
感谢Richard展示了如何在r中使用它:
regmatches(str_temp, gregexpr("((?![0-9]+)[A-Za-z0-9]+)", str_temp, perl = TRUE))[[1L]]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句