user3157132 user3157132 - 1 year ago 72
Python Question

Regular expression to separate out the last occurring number using Python

I have a regular expression which separates out the number from the given string.

username = "testuser1"
xp = r'^\D+'
ma = re.match(xp, username)
user_prefix =
print user_prefix

output is


But if the username is something like below

username = "testuser1-1"

I am getting the following output


which is expected. But I am looking for the following


Basically the regular expression should separate out the last occurring whole number (not individual digits).

Summary is

input = "testuser1"
>>> output = testuser
input = "testuser1-1"
>>> output = testuser1-
input = "testuser1-2000"
>>> output = testuser1-

Can I have a single regular expression to deal with the above all cases..?

Answer Source

You can use re.sub and look behind syntax:

re.sub(r'(?<=\D)\d+$', '', username)

A shorter version:

re.sub(r'\d+$', '', username)

The sub function is more suited for this case.

Test cases:

re.sub(r'\d+$', '', "testuser1-100")
# 'testuser1-'

re.sub(r'\d+$', '', "testuser1-1")
# 'testuser1-'

re.sub(r'\d+$', '', "testuser1")
# 'testuser'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download