Hi I have string that could be like this:
%a%%%%%bc%%%d%
I need to replace the first % and the last %, at the start and at the end of the string with "". Also i need to replace all the %+ except the first one for each sequence group of string.
Result should be like this:
a%bc%d
How can I do with regex ? I tried something like this:.*?%(\W+)% but it didn't work
Thanks.
You may use
.replaceAll("^%|%$|(%)+", "$1")
See the regex demo
Details:
^% - the % at the start of the string is matched| - or %$ - a % at the string end| - or(%)+ - a repeated capturing group that matches 1+ % symbols, but captures only one % at each iteration into Group 1, and later, the $1 backreference to this group replaces multiple % chars with 1 %.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With