Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a Regex in Python to remove leading zeros for a number in the middle of a string

Tags:

python

regex

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.

like image 606
Derek O Avatar asked Feb 01 '26 03:02

Derek O


1 Answers

You can use

re.sub(r'^\D*0*', '', text)

See the regex demo. Details

  • ^ - start of string
  • \D* - any zero or more non-digit chars
  • 0* - 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-digits
  • 0* - zero or more zeros
  • (\d+) - Group 1: one or more digits (use (\d+(?:\.\d+)?) to match float or int values)
  • `.* - the rest of the string.

The replacement is the Group 1 value.

like image 126
Wiktor Stribiżew Avatar answered Feb 03 '26 16:02

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!