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

Viewed 1034

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.

3 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.

You may use this re.sub in Python:

string = re.sub(r'^[a-zA-Z]*0*|[a-zA-Z]+', '', string)

RegEx Demo

Explanation:

  • ^: Start
  • [a-zA-Z]*: Match 0 or more letters
  • 0*L: Match 0 or more zeroes
  • |: OR
  • [a-zA-Z]+: Match 1+ of letters

Does this do what you need?

re.sub("[^0-9]+0*", "", "U0123")
>>> '123'
Related