How do we find the exact positions of each match in Python regular expression?

PythonServer Side ProgrammingProgramming

We use the re.finditer() method to find the exact positions of each match in given string using Python regex

Example

import re
p = re.compile("[A-Z0-9]")
for m in p.finditer('A5B6C7D8'):
    print m.start(), m.group()

Output

This gives the output

0 A
1 5
2 B
3 6
4 C
5 7
6 D
7 8

raja
Updated on 20-Feb-2020 07:43:47

Advertisements