import re
telNumber = '''Suppose my Phone No. is 0535-1234567,yours is 010-12345678,his is 025-87654321.'''
pattern = re.compile(r'(\d{3,4})-(\d{7,8})')
index = 0
while True:
matchResult=pattern.search(telNumber,index) #从指定位置开始匹配
if not matchResult:
break
print('-'*30)
print('Success:')
for i in range(3):
print('Search content:',matchResult.group(i),'Start from:',matchResult.start(i),'End at:',matchResult.end(i),'Its span is:',matchResult.span(i))
index=matchResult.end(2) #指定下次匹配的开始位置
# ------------------------------
# Success:
# Search content: 0535-1234567 Start from: 24 End at: 36 Its span is: (24, 36)
# Search content: 0535 Start from: 24 End at: 28 Its span is: (24, 28)
# Search content: 1234567 Start from: 29 End at: 36 Its span is: (29, 36)
# ------------------------------
# Success:
# Search content: 010-12345678 Start from: 46 End at: 58 Its span is: (46, 58)
# Search content: 010 Start from: 46 End at: 49 Its span is: (46, 49)
# Search content: 12345678 Start from: 50 End at: 58 Its span is: (50, 58)
# ------------------------------
# Success:
# Search content: 025-87654321 Start from: 66 End at: 78 Its span is: (66, 78)
# Search content: 025 Start from: 66 End at: 69 Its span is: (66, 69)
# Search content: 87654321 Start from: 70 End at: 78 Its span is: (70, 78)