在字符串中正则表达式匹配后插入计数器

时间:2015-02-20 07:46:57

标签: regex r perl

在字符串

中匹配后插入计数器

我正在尝试在字符串中的每次匹配后插入一个count-suffix。

例如:在以下字符串中每个匹配的“o”之后插入一个数字:

"The Apollo program was conceived early in 1960"

看起来像:

"The Apo1llo2 pro3gram was co4nceived early in 1960"

我想我应该gsub使用perl = TRUE,但不知道如何。

string <- "The Apollo program was conceived early in 1960"

gsub( x = string, pattern = "(o)", replacement = "\\1 $count", perl = TRUE )

4 个答案:

答案 0 :(得分:4)

这是一种方法:

x <- "The Apollo program was conceived early in 1960"

library(stringi)  ## or
pacman::p_load(stringi)  ## to load and install if not found

do.call(sprintf, c(list(gsub("o", "o%s", x)), seq_len(stri_count_regex(x, "o"))))

## [1] "The Apo1llo2 pro3gram was co4nceived early in 1960"

或者更简洁:

pacman::p_load(qdapRegex, stringi)
S(gsub("o", "o%s", x), 1:stri_count_regex(x, "o"))

注意:我维护 pacman qdapRegex 包。

答案 1 :(得分:3)

这是一个使用gregexprregmatchesregmatches<-的强大组合的选项:

x <- c("The Apollo program was conceived early in 1960",
       "The International Space Station was launched in 1998")

m <- gregexpr("(?<=o)", x, perl=TRUE)
regmatches(x,m) <- lapply(regmatches(x,m), seq_along)

x
# [1] "The Apo1llo2 pro3gram was co4nceived early in 1960"    
# [2] "The Internatio1nal Space Statio2n was launched in 1998"

答案 2 :(得分:3)

在@Josh O&#39; Brien的gsubfn上使用x的另一种可能性。

library(gsubfn)
p <- proto(fun = function(this, x) paste0(x, count))
gsubfn("o", p, x)

# [1] "The Apo1llo2 pro3gram was co4nceived early in 1960"    
# [2] "The Internatio1nal Space Statio2n was launched in 1998"

如需进一步阅读,请参阅不错的gsubfn vignette

答案 3 :(得分:1)

当您将此问题标记为Perl时,这是一个Perl解决方案。

$string = "The Apollo program was conceived early in 1960";
$string =~ s/o/o . ++$i/eg;
say $string;