匹配R中的多个正则表达式

时间:2015-10-04 15:34:18

标签: regex r text

我不确定此问题是否已经解决过,至少是直接形式。

我有一个这样的段落:

“尽管我努力安抚他的努力都失败了,但我仍然希望他能继续珍惜我们的友谊。过去18年来,我们一直是好朋友,我不知道为什么会有一个简单的意见分歧他太生气了!好吧,生活很奇怪,并且有其道路,我想“

我想在文章中进行以下替换:

 "While all my efforts" -> <my-attempt>
 "to pacify him" -> <to-make-things-better>
 "failed" -> <failure>
 "I still hoped" -> <hope>
 "we had been great friends" -> <we-were-friends>
 "so mad" -> <unhappy>

其余文字可保持原样。

是否可以通过一次调用正则表达式来完成此操作 R?中的函数

谢谢!

1 个答案:

答案 0 :(得分:3)

请参阅此postanswer - 来自@TheodoreLytras:

text <- c("While all my efforts to pacify him failed, I still hoped that he would continue to value our friendship. We had been great friends for the past 18 years, and I don't know why a simple difference of opinion should make him so mad! Well, life is strange, and has its ways, I guess")

mgsub <- function(pattern, replacement, x, ...) {
  if (length(pattern)!=length(replacement)) {
    stop("pattern and replacement do not have the same length.")
  }
  result <- x
  for (i in 1:length(pattern)) {
    result <- gsub(pattern[i], replacement[i], result, ...)
  }
  result
}

patterns <- c("While all my efforts",
              "to pacify him",
              "failed",
              "I still hoped",
              "we had been great friends",
              "so mad")

replacements <- c("<my-attempt>",
                  "<to-make-things-better>",
                  "<failure>",
                  "<hope>",
                  "<we-were-friends>",
                  "<unhappy>")

mgsub(pattern = patterns, replacement = replacements, x = text)
# [1] "<my-attempt> <to-make-things-better> <failure>, <hope> that he would continue to value our friendship. We had been great friends for the past 18 years, and I don't know why a simple difference of opinion should make him <unhappy>! Well, life is strange, and has its ways, I guess"