Regex match occurrences only after character sequence

↘锁芯ラ 提交于 2019-12-31 03:21:33

问题


I want to find all the characters after w's that occur in this string, but only the ones after foo

edward woodward foo edward woodward

/(?<=w)./g gives me 6 matches

edward woodward foo edward woodward

I only want the 3 matches that occur after foo. How would I modify the regex to narrow the scope of the search?


回答1:


You may use the following regex with a PCRE engine:

(?:\bfoo\b|\G(?!^))[^w]*w\K.

See the regex demo.

Details

  • (?:\bfoo\b|\G(?!^)) - either a whole word foo (\bfoo\b) or (|) the end of the previous match (\G(?!^))
  • [^w]* - any 0+ chars other than w
  • w - a w char
  • \K - match reset operator discarding all text matched so far
  • . - any char (other than line break chars, if you need to match them with . add (?s) at the pattern start or replace . with (?s:.))


来源:https://stackoverflow.com/questions/48875506/regex-match-occurrences-only-after-character-sequence

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