Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading