Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีหาตำแหน่งที่แน่นอนของแต่ละ match ใน Python Regex

ใช้ re.finditer() หาตำแหน่งที่แน่นอนของแต่ละ match ในสตริงที่กำหนด

ตัวอย่าง

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

ผลลัพธ์

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