如何在R中使用带正则表达式的str_split?
我有这个字符串:如何在R中使用带正则表达式的str_split?,r,regex,stringr,strsplit,R,Regex,Stringr,Strsplit,我有这个字符串: 235072,testing,some252f4,14084-things224072,and,other2524,14084-thingies223552,testing,some/2wr24,14084-things 我想把字符串按6位数字分开。我想要这个: 235072,testing,some2wg2f4,wf484-things 224072,and,other25wg4,14-thingies 223552,testing,some/2wr24,14084-thi
235072,testing,some252f4,14084-things224072,and,other2524,14084-thingies223552,testing,some/2wr24,14084-things
我想把字符串按6位数字分开。我想要这个:
235072,testing,some2wg2f4,wf484-things
224072,and,other25wg4,14-thingies
223552,testing,some/2wr24,14084-things
我该如何使用正则表达式?以下操作不起作用(使用stringr
package):
blahblah测试
[[1]]
[1] "" ""
我遗漏了什么???这里有一种使用正向前瞻和后向前瞻的基本R方法,感谢@thelatemail的更正:
strsplit(x, "(?<=.)(?=[0-9]{6})", perl = TRUE)[[1]]
# [1] "235072,testing,some252f4,14084-things"
# [2] "224072,and,other2524,14084-thingies"
# [3] "223552,testing,some/2wr24,14084-things"
strsplit(x,”(?一种使用str\u extract\u all
的替代方法。注意,我已经使用*?
进行了“非贪婪”匹配,否则*
将展开以获取所有内容:
> str_extract_all(blahblah, "[0-9]{6}.*?(?=[0-9]{6}|$)")[[1]]
[1] "235072,testing,some252f4,14084-things" "224072,and,other2524,14084-thingies" "223552,testing,some/2wr24,14084-things"
一种简单易懂的方法是添加一个标记,然后根据这些标记的位置进行拆分。这样做的优点是,只能查找6位数的序列,而不需要周围文本中的任何其他功能,这些功能可能会随着添加新的和未录制的数据而改变
library(stringr)
library(magrittr)
str <- "235072,testing,some252f4,14084-things224072,and,other2524,14084-thingies223552,testing,some/2wr24,14084-things"
out <-
str_replace_all(str, "(\\d{6})", "#SPLIT_HERE#\\1") %>%
str_split("#SPLIT_HERE#") %>%
unlist
[1] "" "235072,testing,some252f4,14084-things"
[3] "224072,and,other2524,14084-thingies" "223552,testing,some/2wr24,14084-things"
使用不太复杂的正则表达式,可以执行以下操作:
s <- "235072,testing,some252f4,14084-things224072,and,other2524,14084-thingies223552,testing,some/2wr24,14084-things"
l <- str_locate_all(string = s, "[0-9]{6}")
str_sub(string = s, start = as.data.frame(l)$start,
end = c(tail(as.data.frame(l)$start, -1) - 1, nchar(s)) )
# [1] "235072,testing,some252f4,14084-things"
# [2] "224072,and,other2524,14084-thingies"
# [3] "223552,testing,some/2wr24,14084-things"
s通过在正则表达式中使用*
可以允许它匹配任何内容。因此,拆分正则表达式扩展到字符串的整个长度,然后当您拆分字符串的任一侧时,在开始和结束处都会出现两个空字符串。谢谢!这解决了问题。不过,我刚刚意识到我有一些7位数的数字在处理字符串p、 你有办法排除7位数字,但继续按6位数字进行解析??谢谢!该死的,我讨厌正则表达式。你推荐任何好的介绍??每次我想我明白了,我就在这里结束了……你的解决方案切断了前两行的最后一个字符。为什么会这样?@lukehawk,问题是最后的字符被用于拆分。至于介绍,我不确定。在这里练习和回答问题很有效。@lukehawk,对于你的新问题,请尝试strsplit(x,“(?谢谢Julius-我不知道这有什么关系,但是这个字符串非常大,大约50MB。我已经切换到str_extract_all
解决方案,因为它似乎能够更好地处理大字符串。我实际上使用了这个解决方案,因为它似乎可以更好地处理我的非常大的字符串(49MB)。但是,我现在如何调整它以接受6位或3位(但不是任何其他数量)整数作为分隔符呢?这是有效的str_extract_all(mystring,[0-9]*.*(?=\\s{5}[0-9]{3}{124; \\ s{5}[0-9]{6}$)
,但它在每个实字符串之后添加一个空字符串,所以我得到的结果是原来的两倍。知道为什么吗?明白了!这非常有效-stru-extract-all(mystring),(\\d{6}\\d{3})。*?(?=\\s{5}[0-9]{3}\\\s{5}[0-9]{6}[1]
谢谢大家的帮助!我想因为你只有(
)lookahead在这一点上,它允许在lookahead匹配的点上进行零长度匹配。但是,在评论中很难解决这一问题,最好写一个新问题,并给出新问题的明确示例(只要你有好的示例来说明它与此问题的区别,就可以了)。
[1] "235072,testing,some252f4,14084-things" "224072,and,other2524,14084-thingies"
[3] "223552,testing,some/2wr24,14084-things"
s <- "235072,testing,some252f4,14084-things224072,and,other2524,14084-thingies223552,testing,some/2wr24,14084-things"
l <- str_locate_all(string = s, "[0-9]{6}")
str_sub(string = s, start = as.data.frame(l)$start,
end = c(tail(as.data.frame(l)$start, -1) - 1, nchar(s)) )
# [1] "235072,testing,some252f4,14084-things"
# [2] "224072,and,other2524,14084-thingies"
# [3] "223552,testing,some/2wr24,14084-things"