1.代码行之中的index具体是什么意思呢?是相当于whence调整移动字节的参考位置的这个意思吗?
2.“这段代码中的 telNumber 变量是一个多行字符串,其中包含了多个电话号码。”,我不明白telnumber在这儿不是三个单引号引用的部分吗(我以为只有注释的意思,为什么能识别里面的内容?这个问题有点傻) 并且怎么“如果想要对单个电话号码进行匹配,可以将其作为一个普通字符串传入正则表达式函数中” ?又要怎么改呢?
3.输出语句中 group()的用法让我大为震惊 (我是初学者)不明白0,1,2为什么可以得到三种不同的结果 可否指路让我去自行学习这种方法?
```python
print(matchResult.group(0))
这段代码会遍历 telNumberList 中的每一个匹配结果,并输出其完整的匹配字符串。matchResult.group(0) 表示匹配结果的第一个分组,即完整的电话号码。如果想要输出电话号码的前三个数字和后四个数字,可以将 group(0) 改为 group(1) 和 group(2),分别表示第一个和第二个分组。
```完整的代码如下:
import re
telNumber='''Suppose my Phone No. is 028-32132123,yours is 010-12343244,his is 025-78659811.'''
pattern=re.compile(r'(\d{3,4})-(\d{7,8})')
index=0
telNumberList=[]
while True:
matchResult=pattern.search(telNumber,index)
if not matchResult:
break
telNumberList.append(matchResult)
index=matchResult.end(2)
for matchResult in telNumberList:
print(matchResult.group(0))
```