9951 explained code solutions for 126 technologies


python-regexHow to match digits with Python regex?


Python regex can be used to match digits with the \d character class. This character class matches any single digit from 0 to 9.

For example:

import re

string = '12345'

match = re.search(r'\d', string)

if match:
    print(match.group())

Output example

1

The code above uses the re.search() function to search for a single digit in the string 12345. The \d character class is used as the pattern to match. The match.group() function is then used to print the first digit that is matched.

Code explanation

  • import re: imports the re module which contains the functions needed for regex operations
  • string = '12345': assigns the string 12345 to the variable string
  • re.search(r'\d', string): searches for a single digit in the string 12345 using the \d character class as the pattern
  • match.group(): prints the first digit that is matched

Helpful links

Edit this code on GitHub