user3157132 user3157132 - 5 months ago 16
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 = ma.group(0)
print user_prefix


output is

testuser


But if the username is something like below

username = "testuser1-1"


I am getting the following output

testuser


which is expected. But I am looking for the following

testuser1-


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

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'