Match numbers
r|re \d group|re
import re
line = 'There is a phone number 12345 in this row and an age: 23'
match = re.search(r'\d+', line)
if match:
print(match.group(0)) # 12345
Use raw strings for regular expression: r'a\d'. Especially because \ needs it.
- \d matches a digit.
-
- is a quantifier and it tells \d to match one or more digits.
It matches the first occurrence.
Here we can see that the group(0)
call is much more interesting than earlier.