I am fairly new to using regex. I have a serial number which can take the following forms: VV-XXXXXX-P or VVXXXXXXP
If the hyphen variant is used, then the number of 'X' can be variable. For example 01-162-8 is equivalent to 010001628.
In order to identify the 2 formats, I have created the following regex's:
String HYPHENS = ([0]{1}[13]{1}-[1-9]{1,6}-[0-9]{1})
String NO_HYPHENS = ([0]{1}[13]{1}[0-9]{6}[0-9]{1})
However the issue with the NO_HYPHENS variant is that it allows 0 anywhere in the sequence For example: 010010628 should not be a valid sequence because there's a non leading 0.
Additionally, how would I create a regex that I can use to replace all 0 from the sequence but the first one? I have tried the following but it also replaces the first 0.
String code = 010001234;
code = code.replaceAll("0+", "");
How could I modify the regex's to achieve this?