Reputation: 7081
Having a bit of trouble coming up with a regex statement for the following room numbers:
C001B --> C1B //remove "leading" zeros after first set of letters
C100B --> C100B
CB001B --> CB1B //remove "leading" zeros after first set of letters
001B --> 1B //remove leading zeros
Essentially, remove all zeros which is not preceded by a numeral?
Upvotes: 2
Views: 647
Reputation: 6849
Find all patterns like 'ABC000'
, '000'
, and eliminate the '0'
s.
return re.sub( "([^0-9]+|^)0+", r'\1', raw )
Upvotes: 0
Reputation: 10786
Well, to match such zeros you could use a negative lookbehind, such as (?<![^a-zA-Z])
, to ensure that the matched area is not preceded by anything but a letter. By inserting this before your match, it will ensure that whatever comes immediately before doesn't match the pattern contained, here [^a-zA-Z]
, which also counts for matches at the very beginning of a line.
re.sub("(?<![^a-zA-Z])0+","",number)
Upvotes: 3