I have the following example string and I would like to remove only one set of zeros.
The example string is
AAL000007200100000787777388300000
I would like to remove only the first set of zero’s and leave the rest intact. I want to end up with
AAL7200100000787777388300000
I have tried the following but it is always too greedy and takes the second set of zeros as well!
#!/usr/bin/env python3
import re
suffixdrop = re.split("\B0+\B", "AAL000007200100000787777388300000", re.I)
if suffixdrop: #found a suffix here
print(suffixdrop)
The result of the print is
[‘AAL’, ’72’, ‘100000787777388300000’]
\Bmatches the opposite of word boundaries, which doesn’t sound like what you want here.If you want to remove the first set of consecutive 0’s after a non-digit character, try this:
re.sub("^(\D+)0+", r"\1", x)means “match the start of the line followed by any number of non-digit characters, followed by any number of 0 characters” and replace it with the first group of non-digit characters.