Computer >> Computer tutorials >  >> Programming >> Python

How to match only digits in Python using Regular Expression?


The following code matches only digits in the given string using python regular expression.

Example

import re
m = re.search(r'\d+', '5Need47forSpeed 2')
print m

output

<_sre.SRE_Match object at 0x0000000004B46648>

Example

The following code finds all digits in the given string and prints them as a list

import re
m = re.findall(r'\d', '5Need47forSpeed 2')
print m

output

['5', '4', '7', '2']