DreadAngel DreadAngel - 18 days ago 4
Java Question

RegEx - finding group at the end of the line

Expression does not match if it is located at the end of the line:

(\b%%\b([^%])*\b%%\b)


test string:

djwk%%text%%doqdojd%%text.text.text%%kjwldjdflkljwfe%%body.text.text%%


as result:

djwk_doqdojd_kjwldjdflkljwfe%%body.text.text%%


but the last one is missed (tested via)

What I'm missing?

Answer

Assuming that you would like to have a result like:

djwk_doqdojd_kjwldjdflkljwfe_

Then, just try the follow regex:

(\b%%\b([^%])*\b%%)

Explanation:

The final "\b" on the regex you have provided, forces that the character "%%" can't be the last element on the target string.

Hope this helps you.

Cheers, Nikolas.