简体   繁体   English

Java正则表达式匹配相同特定字符序列之间的所有内容

[英]Java regular expression to match everything between same specific character sequences

Let's take string "Something foo part1 foo part2 foo part3" as an example. 我们以字符串"Something foo part1 foo part2 foo part3"为例。

I want to find all parts starting with "foo" continuing till another "foo" or end of string and replace (wrap inner part into HTML markup) afterwards. 我想找到以“foo”开头的所有部分,直到另一个“foo”或字符串结尾,然后替换(将内部部分包装成HTML标记)。 So the result should be "Something <bar> part1 </bar><bar> part2 </bar><bar> part3</bar>" 所以结果应该是"Something <bar> part1 </bar><bar> part2 </bar><bar> part3</bar>"

I've started with: "foo(.*?)(foo|$)" and replacing it with "<bar>$1</bar>" . 我开始使用: "foo(.*?)(foo|$)"并将其替换为"<bar>$1</bar>" Replacing seems to be all right but I need help with regex itself. 替换似乎没事,但我需要有关正则表达式本身的帮助。

I have tried many variations with negative lookbehind and others so far without success. 到目前为止,我已经尝试了很多带有负面观察和其他变化的变化。 Thanks for any suggestions. 谢谢你的任何建议。

Just change your second group into a lookahead 只需将第二组更改为预测即可

foo(.*?)(?=foo|$)

See it on Regexr 在Regexr上看到它

The problem is you are matching the "foo" that you want to use as next start point. 问题是你要匹配你想用作下一个起点的“foo”。 You can avoid this by using the lookahead assertion . 您可以通过使用前瞻断言来避免这种情况 This way the following "foo" is not matched and therefor used as start of the next match. 这样,以下“foo”不匹配,因此用作下一场比赛的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM