I would like to match a word if there a specific word before in a sentence, for instance:
test1 foo test2 bar
I would like to match bar
if foo
is in the sentence before (and/or after bar
).
I tried:
(?<=(foo ([\w ]+)(?= bar)))bar
But doesn't work, the regex is incorrect because of +
in lookbehind...
Could you please help me, thanks
If you have to use one regex, you can just simply move the [\\w ]+
out of the lookbehind. Then use an alternation to look for the word bar (on its own, not as part of another word) followed by foo as well:
((?<=foo)[\w ]+\bbar\b|\bbar\b[\w ]+(?=foo))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.