Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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.