I would like to remove Zero from a string, below is an example :
String a : 020200218-0PSS-0010
a.replaceall("-(?:.(?!-))+$", "**REPLACEMENT**")
Actual : 020200218-0PSS-0010
Expected : 020200218-0PSS-10
I'm using this regex to catch -0010 : -(?:.(?!-))+$
I just dont know what to do in the REPLACEMENT section to actually remove the unused zero (not the last zero for exemple "10" and not "1")
Thanks for reading!
You could use something like:
(?<=-)0+(?=[1-9]\d*$)
It translates to "find all zeros which come after a dash, leading up to a non-zero digit, followed by optional digits, till the end of the string."
The demo below is in PHP but it should be valid in Java as well.
https://regex101.com/r/7E2KKQ/1
$s.replaceAll("(?<=-)0+(?=[1-9]\d*$)", "")
This would also work:
(?<=-)(?=\d+$)0+
Find a position in which behind me is a dash and ahead of me is nothing but digits till the end of the line. From this position match one or more continuous zeros.