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