首页 > 解决方案 > 查找不在字符串开头的子字符串

问题描述

我想查找并替换 a 的子字符串string,但前提是该子字符串不在字符串的开头。我试过下面的代码,它给出了这些错误:

string <- c("some text followed by substring then more text followed by substring and then lots of text with lots of examples of substring and then more text", "substring at the beginning example with substring later in the string and then another substring blah blah, blah")
gsub("(?<!^)substring", "replacement", string)
Error in gsub("(?<!^)substring", "replacement", string) : 
  invalid regular expression '(?<!\^)substring', reason 'Invalid regexp'
gsub("(?<!\\^)substring", "replacement", string)
Error in gsub("(?<!\\^)substring", "replacement", string) : 
  invalid regular expression '(?<!\^)substring', reason 'Invalid regexp'

我正在使用以下代码,该代码有效但似乎不“正确”:

gsub("(.+?)substring", "replacement", string)

这是最好的选择吗?

标签: rregexregex-lookarounds

解决方案


问题是perl = TRUE默认情况下FALSE根据?gsub

gsub(模式,替换,x,ignore.case = FALSE,perl = FALSE,fixed = FALSE,useBytes = FALSE)

gsub("(?<!^)substring", "replacement", string, perl = TRUE)
#[1] "some text followed by replacement then more text followed by replacement and then lots of text with lots of examples of replacement and then more text"
#[2] "substring at the beginning example with replacement later in the string and then another replacement blah blah, blah"                                  

推荐阅读