Skip to content
Advertisement

Allow only left aligned zeros using regex

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?

Advertisement

Answer

You can use

String NO_HYPHENS = "0[13](?!0*[1-9]0)[0-9]{6}[0-9]";
code = code.replaceAll("(?!^)0(?!$)", "");

See the regex demo. The 0[13](?!0*[1-9]0)[0-9]{6}[0-9] regex matches:

  • 0 – zero
  • [13] – one or three
  • (?!0*[1-9]0) – any zeros followed with a non-zero digit and then a zero is not allowed at this location
  • [0-9]{6} – six digits
  • [0-9] – a digit.

I understand you use it in Java with .matches, else, add ^ at the start and $ at the end.

Also, the (?!^)0(?!$) regex will match any zero that is not at the string start/end position.

Advertisement