问题
I only want to replace string occurrences that follow a particular keyword/pattern and not before. in other words, do nothing until the first occurrence of the keyword-pattern, and then start to gsub to the right of that keyword-pattern. See below:
gsub("\\[|\\]", "", "ab[ cd] ef keyword [ gh ]keyword ij ")
Actual results: "ab cd ef keyword gh keyword ij "
Desired results: "ab[ cd] [][asfg] ]] ef keyword gh keyword ij "
[Edited to fix the results. I don't want to remove 'keyword'] [Edited to show case of multiple occurrences of keyword]
回答1:
You might use \G to get continous matches after keyword. Use \K to forget what was matched and match the following [ or ] to be replaced with an empty string.
(?:^.*?keyword\b|\G(?!^))[^\[\]]*\K[\[\]]
In parts
(?:Non capturing group^.*?keywordMatch until the first keyword|Or\G(?!^)Assert position at the end of previous match, not at the start to get continuous matches
)Close non capturing group[^\[\]]*\KMatch 0+ times not[or]and forget what was matched using\K[\[\]]Match either[or]
Regex demo | R demo
Your code might look like
gsub("(?:^.*?keyword\\b|\\G(?!^))[^\\[\\]]*\\K[\\[\\]]", "", "ab[ cd] ef keyword [ gh ]keyword ij ", perl=T)
Note to use perl=T at the end for Perl-like regular expressions.
来源:https://stackoverflow.com/questions/58562449/r-gsub-replace-only-those-occurrences-following-a-keyword-occurrence