I have a string composed of both letters followed by a number, and I need to remove all the letters, as well as the leading zeros in the number.
For example: in the test string U012034, I want to match the U and the 0 at the beginning of 012034.
So far I have [^0-9] to match the any characters that aren't digits, but I can't figure out how to also remove the leading zeros in the number.
I know I could do this in multiple steps with something like int(re.sub("[^0-9]", "", test_string) but I need this process to be done in one regex.
You can use
re.sub(r'^\D*0*', '', text)
See the regex demo. Details
^ - start of string\D* - any zero or more non-digit chars0* - zero or more zeros.See Python demo:
import re
text = "U012034"
print( re.sub(r'^\D*0*', '', text) )
# => 12034
If there is more text after the first number, use
print( re.sub(r'^\D*0*(\d+).*', r'\1', text) )
See this regex demo. Details:
^ - start of string\D* - zero or more non-digits0* - zero or more zeros(\d+) - Group 1: one or more digits (use (\d+(?:\.\d+)?) to match float or int values)The replacement is the Group 1 value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With