0

I want to find and replace a substring of a string, but only if the substring is NOT at the beginning of the string. I've tried the code below, which gives these errors:

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'

I'm using the following code, which works but doesn't seem "right":

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

Is this the best option?

Josh
  • 1,210
  • 12
  • 30
  • 1
    What is `string` here. In the first three cases, you need `perl = TRUE` – akrun Jan 25 '20 at 22:13
  • Argh, `perl = TRUE` kills me every time!! I just added an example of `string` – Josh Jan 25 '20 at 22:16
  • 2
    As @Wiktor Stribiżew noted, this question has been explicitly asked and answered before (shakes fist at item #6 in search list) and has been touched upon in multiple comments or answers https://stackoverflow.com/questions/26573611/r-regex-lookbehind-lookahead-issue https://stackoverflow.com/questions/40251836/regex-negative-lookbehind-in-r https://stackoverflow.com/questions/58596135/r-regex-lookbehind-with-a-long-expression https://stackoverflow.com/questions/33930738/lookbehind-to-get-the-text-in-r-regex – Josh Jan 25 '20 at 22:33

1 Answers1

1

The issue is with perl = TRUE which is by default FALSE according to ?gsub

gsub(pattern, replacement, 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"                                  
akrun
  • 874,273
  • 37
  • 540
  • 662