Python中re的match、search、findall、finditer区别 Python中re的match、search、findall、finditer区别

Python中re的match、search、findall、finditer区别

这四个方法是从某个字符串中寻找特定子串判断某个字符串是否符合某个模式的常用方法。

1、match

[python]  view plain  copy
  1. re.match(pattern, string[, flags])  
从首字母开始开始匹配,string如果包含pattern子串,则匹配成功,返回Match对象,失败则返回None,若要完全匹配,pattern要以$结尾。

2、search

[python]  view plain  copy
  1. re.search(pattern, string[, flags])  
若string中包含pattern子串,则返回Match对象,否则返回None,注意,如果string中存在多个pattern子串,只返回第一个。

3、findall

[python]  view plain  copy
  1. re.findall(pattern, string[, flags])  
返回string中所有与pattern相匹配的全部字串,返回形式为数组。

4、finditer

[python]  view plain  copy
  1. re.finditer(pattern, string[, flags])  
返回string中所有与pattern相匹配的全部字串,返回形式为迭代器。


若匹配成功,match()/search()返回的是Match对象,finditer()返回的也是Match对象的迭代器,获取匹配结果需要调用Match对象的group()、groups或group(index)方法。

group()、groups()与group(index)的区别,如下所示:

[python]  view plain  copy
  1. >>> import re  
  2. >>> s = '23432werwre2342werwrew'  
  3. >>> p = r'(\d*)([a-zA-Z]*)'  
  4. >>> m = re.match(p,s)  
  5. >>> m.group()  
  6. '23432werwre'  
  7. >>> m.group(0)  
  8. '23432werwre'  
  9. >>> m.group(1)  
  10. '23432'  
  11. >>> m.group(2)  
  12. 'werwre'  
  13. >>> m.groups()  
  14. ('23432''werwre')  
  15. >>> m = re.findall(p,s)  
  16. >>> m  
  17. [('23432''werwre'), ('2342''werwrew'), ('''')]  
  18. >>> p=r'(\d+)'  
  19. >>> m=re.match(p,s)  
  20. >>> m.group()  
  21. '23432'  
  22. >>> m.group(0)  
  23. '23432'  
  24. >>> m.group(1)  
  25. '23432'  
  26. >>> m.groups()  
  27. ('23432',)  
  28. >>> m=re.findall(p,s)  
  29. >>> m  
  30. ['23432''2342']  
综上:

  • group():母串中与模式pattern匹配的子串;
  • group(0):结果与group()一样;
  • groups():所有group组成的一个元组,group(1)是与patttern中第一个group匹配成功的子串,group(2)是第二个,依次类推,如果index超了边界,抛出IndexError;
  • findall():返回的就是所有groups的数组,就是group组成的元组的数组,母串中的这一撮组成一个元组,那一措组成一个元组,这些元组共同构成一个list,就是findall()的返回结果。另,如果groups是只有一个元素的元组,findall的返回结果是子串的list,而不是元组的list了。

例子

[sql]  view plain  copy
  1. s ="1113446777"  
用正则表达式把s分为1111, 3, 44, 6, 777
[python]  view plain  copy
  1. >>> import re  
  2. >>> s='1113446777'  
  3. >>> m = re.findall(r'(\d)\1*',s)  
  4. >>> print m  
  5. ['1''3''4''6''7']  
  6. >>> m = re.search(r'(\d)\*',s)  
  7. >>> m.group()  
  8. >>> m=re.search(r'(\d)\1*',s)  
  9. >>> m.group()  
  10. '111'  
  11. >>> m.groups()  
  12. ('1',)  
  13. >>> m.group(0)  
  14. '111'  
  15. >>> m.group(1)  
  16. '1'  
  17. >>> m.group(2)  
  18. Traceback (most recent call last):  
  19.   File "<stdin>", line 1in <module>  
  20. IndexError: no such group  
  21. >>> m=re.finditer(r'(\d)\1*',s)  
  22. >>> m.next().group()  
  23. '111'  
  24. >>> m.next().group()  
  25. '3'  
  26. >>> m.next().group()  
  27. '44'  
  28. >>> m.next().group()  
  29. '6'  
  30. >>> m.next().group()  
  31. '777'  
  32. >>> m.next().group()  
  33. Traceback (most recent call last):  
  34.   File "<stdin>", line 1in <module>  
  35. StopIteration  

另一个例子:

[python]  view plain  copy
  1. >>> p = r'(\d)\1+([a-zA-Z]+)'  
  2. >>> s = '1111werwrw3333rertert4444'  
  3. >>> p = r'(\d)\1+([a-zA-Z]*)'  
  4. >>> import re  
  5. >>> re.findall(p,s)  
  6. [('1''werwrw'), ('3''rertert'), ('4''')]  
  7. >>> m = re.search(p,s)  
  8. >>> m.group()  
  9. '1111werwrw'  
  10. >>> m.group(1)  
  11. '1'  
  12. >>> m.group(2)  
  13. 'werwrw'  
  14. >>> m.groups()  
  15. ('1''werwrw')  
  16. >>> m = re.finditer(p,s)  
  17. >>> m.next().group()  
  18. '1111werwrw'  
  19. >>> m.next().group()  
  20. '3333rertert'  
  21. >>> m.next().group()  
  22. '4444'  
  23. >>> m.next().group()  
  24. Traceback (most recent call last):  
  25.   File "<stdin>", line 1in <module>  
  26. StopIteration  

猜你喜欢

转载自blog.csdn.net/hzp666/article/details/80163141