I have the following string:
s = Promo 77
I am trying to get the output "77".
The following is the regex I am using:
>>> re.sub(r'^[0-9]','',s)
'Promo 77'
What should the correct statement be here?
I have the following string:
s = Promo 77
I am trying to get the output "77".
The following is the regex I am using:
>>> re.sub(r'^[0-9]','',s)
'Promo 77'
What should the correct statement be here?
>>> s = 'Promo 77'
>>> "".join(i for i in s if i.isdigit())
'77'
You simply need to move the ^
. r'^[0-9]'
matches the beginning of the string, followed by a digit (which does not appear in your string). You want r'[^0-9]'
, which matches any character that is not a digit, or r'\D'
, which matches exactly the same set of characters.
s = "Promo 77"
print re.findall("\d+",s)
['77']
print s.split()[-1]
77
re.sub(r'\D', "", s)
77
re.sub(r"[^\d+]","",s)
77