0

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?

David542
  • 101,766
  • 154
  • 423
  • 727

3 Answers3

4
>>> s = 'Promo 77'
>>> "".join(i for i in s if i.isdigit())
'77'
Cory Kramer
  • 107,498
  • 14
  • 145
  • 201
2

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.

murgatroid99
  • 17,055
  • 9
  • 55
  • 92
1
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
Padraic Cunningham
  • 168,988
  • 22
  • 228
  • 312