如何替换字符串中的匹配并索引每个匹配项

时间:2017-11-02 15:33:07

标签: r string substring stringr stringi

特定字符串可以包含我尝试匹配的模式的多个实例。例如,如果我的模式为<N(.+?)N>且我的字符串为"My name is <N Timon N> and his name is <N Pumba N>",则有两个匹配项。我想用包含正在替换匹配的索引的替换替换每个匹配。

所以在我的字符串"My name is <N Timon N> and his name is <N Pumba N>"中, 我想将字符串更改为"My name is [Name #1] and his name is [Name #2]"

如何实现这一目标,最好只使用一个功能?最好使用stringrstringi

中的函数

4 个答案:

答案 0 :(得分:3)

您可以使用Base R中的gregexprregmatches执行此操作:

my_string = "My name is <N Timon N> and his name is <N Pumba N>"

# Get the positions of the matches in the string
m = gregexpr("<N(.+?)N>", my_string, perl = TRUE)

# Index each match and replace text using the indices
match_indices = 1:length(unlist(m))

regmatches(my_string, m) = list(paste0("[Name #", match_indices, "]"))

<强>结果:

> my_string
# [1] "My name is [Name #1] and his name is [Name #2]"

注意:

如果多次出现,此解决方案会将同一匹配视为不同的“名称”。例如以下内容:

my_string = "My name is <N Timon N> and his name is <N Pumba N>, <N Timon N> again"


m = gregexpr("<N(.+?)N>", my_string, perl = TRUE)

match_indices = 1:length(unlist(m))

regmatches(my_string, m) = list(paste0("[Name #", match_indices, "]"))

输出:

> my_string
[1] "My name is [Name #1] and his name is [Name #2], [Name #3] again"

答案 1 :(得分:2)

这是一个依赖于gsubfnproto软件包的解决方案。

# Define the string to which the function will be applied
my_string <- "My name is <N Timon N> and his name is <N Pumba N>"

# Define the replacement function
replacement_fn <- function(x) {

  replacment_proto_fn <- proto::proto(fun = function(this, x) {
      paste0("[Name #", count, "]")
  })

  gsubfn::gsubfn(pattern = "<N(.+?)N>",
                 replacement = replacment_proto_fn,
                 x = x)
}

# Use the function on the string
replacement_fn(my_string)

答案 2 :(得分:1)

以下是使用dplyr + stringr

的不同方法
library(dplyr)
library(stringr)

string %>%
  str_extract_all("<N(.+?)N>") %>%
  unlist() %>%
  setNames(paste0("[Name #", 1:length(.), "]"), .) %>%
  str_replace_all(string, .)

# [1] "My name is [Name #1] and his name is [Name #2]"

注意:

第二个解决方案使用str_extract_all提取匹配项,然后使用匹配项创建一个命名的替换向量,最后将其输入str_replace_all以进行相应的搜索和替换。

正如OP所指出的,在某些情况下,此解决方案会产生与gregexpr + regmatches方法不同的结果。例如以下内容:

string = "My name is <N Timon N> and his name is <N Pumba N>, <N Timon N> again"

string %>%
  str_extract_all("<N(.+?)N>") %>%
  unlist() %>%
  setNames(paste0("[Name #", 1:length(.), "]"), .) %>%
  str_replace_all(string, .)

输出:

[1] "My name is [Name #1] and his name is [Name #2], [Name #1] again"

答案 3 :(得分:0)

简单,可能很慢,但应该有效:

ct <- 1
while(TRUE) {
 old_string <- my_string; 
 my_string <- stri_replace_first_regex(my_string, '\\<N.*?N\\>', 
       paste0('[name', ct, ,']')); 
  if (old_string == my_string) break 
  ct <- ct + 1
}