Home > Enterprise >  How do I trim leading zeros from a regular expression capture group?
How do I trim leading zeros from a regular expression capture group?

Time:09-02

I have a regular expression that splits an 18 digit number into 4 capture groups (saved at regex101.com).

/(\d{5})(\d{2})(\d{2})(\d{9})/mg

Using 000012022000456789 as the test string, my result is:

Group 1: 00001
Group 2: 20
Group 3: 22
Group 4: 000456789

I also need to trim leading zeros from Group 1, so my desired result is:

Group 1: 1
Group 2: 20
Group 3: 22
Group 4: 000456789

Can this all be done using one regular expression? Note that this is a general regular expression question, not specific to an engine or language.

CodePudding user response:

You could add a non-capturing group to absorb up to 4 leading zeros, and adjust your first capturing group to match from 1 to 5 digits:

(?:0{0,4})(\d{1,5})(\d{2})(\d{2})(\d{9})

Demo on regex101

As long as your input is always an 18-digit number, this will work fine. If however the input could be other than 18 digits, this might match something like 01122333333333 or 000001111122333333333.

You can work around this by adding a lookbehind assertion before the second group that requires it to be preceded by exactly 5 digits and an assertion that the string be terminated by a non-digit:

(?:0{0,4})(\d{1,5})(?<=\b\d{5})(\d{2})(\d{2})(\d{9})(?=\b)

Demo on regex101

  • Related