我正在 x 中搜索 4 个不同子字符串的位置,并尝试将这四个输出合并为一个累积字符串:
x <- ("AAABBADSJALKACCWIEUADD")
outputA <- gregexpr(pattern = "AAA", x)
outputB <- gregexpr(pattern = "ABB", x)
outputC <- gregexpr(pattern = "ACC", x)
outputD <- gregexpr(pattern = "ADD", x)
我想合并这四个输出并将这个合并的结果输出为一个文本文件,每个元素在新行中分隔。
merged_output
# 1
# 3
# 13
# 20
谢谢
最佳答案
实际上,您可以使用前瞻(?=)
gregexpr("A(?=AA|BB|CC|DD)", x, perl=T)[[1]]
# [1] 1 3 13 20
# attr(,"match.length")
# [1] 1 1 1 1
# attr(,"useBytes")
# [1] TRUE
关于regex - R将4个字符串合并为1个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31202310/