Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regular expression to match everything between same specific character sequences

Tags:

java

regex

Let's take string "Something foo part1 foo part2 foo part3" as an example.

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. So the result should be "Something <bar> part1 </bar><bar> part2 </bar><bar> part3</bar>"

I've started with: "foo(.*?)(foo|$)" and replacing it with "<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.

like image 851
Radim Vyskup Avatar asked Mar 08 '26 17:03

Radim Vyskup


1 Answers

Just change your second group into a lookahead

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

See it on Regexr

The problem is you are matching the "foo" that you want to use as next start point. 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.

like image 66
stema Avatar answered Mar 11 '26 05:03

stema



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!