I get a 15-digits input string, i.e. 001230123456789.
It has two numbers – the first is 00123 (min. 1 non-zero digit, max. 5 digits), the second is 0123456789 (min. 100000000, max. 10 digits). The captured output (those numbers) should not contain leading zeros.
The easier and, probably, the only proper way to do it (in Python) is the array slicing and lstrip():
input = "001230123456789"
output = [(input[:5].lstrip('0'), input[5:].lstrip('0'))]
# ('123', '123456789')
But I have a task to do the same thing with a regular expression.
I’m stuck with having zome zeros left, whether I’ve tried greedy options or not.
I’ve ended with that one: 0{0,4}([1-9]\d{0,4})0?([1-9]\d{8,9})
and it passes 3/6 of my tests:
000010111111111 - ('10', '111111111') (should be ('1', '111111111'))
116402151672479 - OK ('11640', '2151672479')
006421651672479 - ('6421', '651672479') (should be ('642', '1651672479'))
712120751672479 - OK ('71212', '751672479')
712121551672479 - OK ('71212', '1551672479')
006400351672479 - ('6400', '351672479') (should be ('640', '351672479'))
Is there any way to do it with just a regex?
Full sample source code with a test and expected values on pastie.org.
Here you go.
Here’s a Rubular demo.
(The
(?:^|\s)and(?:\s|$)are for presentation purposes only.)And here are the results: