问题
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 wordfoo(\bfoo\b) or (|) the end of the previous match (\G(?!^))[^w]*- any 0+ chars other thanww- awchar\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