R: gsub/replace only those occurrences following a keyword occurrence

删除回忆录丶 提交于 2019-12-24 21:52:02

问题


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
    • ^.*?keyword Match 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
  • [^\[\]]*\K Match 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!