Regex 如何处理中间词“使用”;“展望未来”;及;“向后看”;用正则表达式?

Regex 如何处理中间词“使用”;“展望未来”;及;“向后看”;用正则表达式?,regex,Regex,根据@skyfoot对这个问题的回答 他说: given the string `foobarbarfoo` bar(?=bar) finds the first bar. bar(?!bar) finds the second bar. (?<=foo)bar finds the first bar. (?<!foo)bar finds the second bar. you can also combine the

根据@skyfoot对这个问题的回答

他说:

given the string `foobarbarfoo`

    bar(?=bar)     finds the first bar.
    bar(?!bar)     finds the second bar.
    (?<=foo)bar    finds the first bar.
    (?<!foo)bar    finds the second bar.

you can also combine them

    (?<=foo)bar(?=bar)    finds the first bar.
给定字符串`foobarbafoo`
bar(?=bar)查找第一个条。
bar(?!bar)查找第二个条。
(?

假设我有字符串“
barfoobarbofoo
”会发生什么

我想找到这些粗体文本

“barfoobarbarfoo”

正则表达式可能是:
(?试试这个模式

foo(?=bar)|(?<=bar)bar  

foo(?=bar)|(?要完成您的示例:

bar(?=bar)     finds the `bar` followed by `bar`
bar(?!bar)     finds the `bar` NOT followed by `bar`
(?<=foo)bar    finds the `bar` preceeded by `foo`
(?<!foo)bar    finds the `bar` NOT preceeded by `foo`

在你接受另一个答案之前,我已经开始思考xD…如果需要更多的调查,我会留下它;-)
(?<=bar)foo(?=bar)|(?<=bar)bar(?=foo)