44357389

来源:互联网 发布:caffe 安装 编辑:程序博客网 时间:2024/06/06 17:04

转载自http://blog.csdn.net/djskl/article/details/44357389

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

1、match

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

2、search

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

3、findall

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

4、finditer

[python] view plain copy
print?
  1. re.finditer(pattern, string[, flags])  
 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
print?
  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’]  
>>> import re>>> s = '23432werwre2342werwrew'>>> p = r'(\d*)([a-zA-Z]*)'>>> m = re.match(p,s)>>> m.group()'23432werwre'>>> m.group(0)'23432werwre'>>> m.group(1)'23432'>>> m.group(2)'werwre'>>> m.groups()('23432', 'werwre')>>> m = re.findall(p,s)>>> m[('23432', 'werwre'), ('2342', 'werwrew'), ('', '')]>>> p=r'(\d+)'>>> m=re.match(p,s)>>> m.group()'23432'>>> m.group(0)'23432'>>> m.group(1)'23432'>>> m.groups()('23432',)>>> m=re.findall(p,s)>>> m['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
print?
  1. s =“1113446777”  
s ="1113446777"
用正则表达式把s分为1111, 3, 44, 6, 777
[python] view plain copy
print?
  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  
>>> import re>>> s='1113446777'>>> m = re.findall(r'(\d)\1*',s)>>> print m['1', '3', '4', '6', '7']>>> m = re.search(r'(\d)\*',s)>>> m.group()>>> m=re.search(r'(\d)\1*',s)>>> m.group()'111'>>> m.groups()('1',)>>> m.group(0)'111'>>> m.group(1)'1'>>> m.group(2)Traceback (most recent call last):  File "<stdin>", line 1, in <module>IndexError: no such group>>> m=re.finditer(r'(\d)\1*',s)>>> m.next().group()'111'>>> m.next().group()'3'>>> m.next().group()'44'>>> m.next().group()'6'>>> m.next().group()'777'>>> m.next().group()Traceback (most recent call last):  File "<stdin>", line 1, in <module>StopIteration

另一个例子:

[python] view plain copy
print?
  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  
>>> p = r'(\d)\1+([a-zA-Z]+)'>>> s = '1111werwrw3333rertert4444'>>> p = r'(\d)\1+([a-zA-Z]*)'>>> import re>>> re.findall(p,s)[('1', 'werwrw'), ('3', 'rertert'), ('4', '')]>>> m = re.search(p,s)>>> m.group()'1111werwrw'>>> m.group(1)'1'>>> m.group(2)'werwrw'>>> m.groups()('1', 'werwrw')>>> m = re.finditer(p,s)>>> m.next().group()'1111werwrw'>>> m.next().group()'3333rertert'>>> m.next().group()'4444'>>> m.next().group()Traceback (most recent call last):  File "<stdin>", line 1, in <module>StopIteration


原创粉丝点击