1

我正在尝试使用 stringi 替换某些单词stri_replace,但是在替换单词的相似部分时遇到了问题。在下面的示例中,我正在修复三角形的拼写错误,但它似乎变得混乱,因为 'tri' 是 'trian' 的一部分,是 'triangle' 的一部分,它就像 'trainglegle'。我不太熟悉stri_replace,有什么我遗漏的论点吗?谢谢你的帮助。

stri_replace_all_regex("The quick brown tri jumped over the lazy trian.",
      c("tri", "trian", "fox"), c("triangle",  "triangle", "bear"), 
         vectorize_all=FALSE)

## [1] "The quick brown trianglegle jumped over the lazy triangleglean."
4

2 回答 2

3

您可能想要隔离单词以使它们不同。\\W是非字符。你可以尝试这样的事情:

stri_replace_all_regex("The quick brown tri jumped over the lazy trian.",
                   paste0(c("trian", "tri",  "fox"), "(\\W)"), 
                   paste0(c("triangle","triangle", "bear"),"$1"),
                   vectorize_all = FALSE)
[1] "The quick brown triangle jumped over the lazy triangle."
于 2016-01-29T21:09:59.437 回答
0

如果您不希望完成部分匹配,则终止一些(甚至可能所有模式参数都用空格(并替换空格:

stri_replace_all_regex("The quick brown tri jumped over the lazy trian.",
  pattern=c("tri "), repl=c("triangle "), 
     vectorize_all=FALSE)

stri_replace_all_regex("The quick brown tri jumped over the lazy trian.",
       c("tri ", "trian", "fox "), c("triangle ",  "triangle", "bear "), 
          vectorize_all=TRUE)
[1] "The quick brown triangle jumped over the lazy trian."
[2] "The quick brown tri jumped over the lazy triangle."  
[3] "The quick brown tri jumped over the lazy trian."     
于 2016-01-29T20:54:58.783 回答