【问题标题】:Find substring not at beginning of string [duplicate]查找不在字符串开头的子字符串[重复]
【发布时间】:2020-01-25 22:13:01
【问题描述】:

我想查找并替换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)

这是最好的选择吗?

【问题讨论】:

标签: r regex regex-lookarounds


【解决方案1】:

问题在于perl = TRUE,根据?gsub,默认情况下FALSE

gsub(模式, 替换, x, ignore.case = FALSE, perl = FALSE, 固定 = 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"                                  

【讨论】:

    猜你喜欢
    • 2014-05-09
    • 2020-02-24
    • 1970-01-01
    • 2014-02-27
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    相关资源
    最近更新 更多