背景
Related question但不是必读
问题
我有一个字符串
str_temp <- "{type: [{a: a1, timestamp: 1}, {a:a2, timestamp: 2}]}"
我想从中提取所有七个字母数字子字符串:type、a、a1、timestamp、a、a2、timestamp
。但是,我无法让我的正则表达式工作。
我已经使用[:word:]、[:alnum:]、[:alpha:]
等的各种组合尝试了基础R和library(stringr)
.
一个例子:
> 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
,时间戳
,而不是起点,或者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]]
关于r - 从文本中提取字母数字字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29550681/