原文地址: http://blog.csdn.net/djskl/article/details/44357389

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

1、match

        re.match(pattern, string[, flags])
从首字母开始开始匹配,string如果包含pattern子串,则匹配成功,返回Match对象,失败则返回None,若要完全匹配,pattern要以$结尾。 2、search re.search(pattern, string[, flags])
若string中包含pattern子串,则返回Match对象,否则返回None,注意,如果string中存在多个pattern子串,只返回第一个。 3、findall re.findall(pattern, string[, flags])
返回string中所有与pattern相匹配的全部字串,返回形式为数组。 4、finditer re.finditer(pattern, string[, flags])
返回string中所有与pattern相匹配的全部字串,返回形式为迭代器。 若匹配成功,match()/search()返回的是Match对象,finditer()返回的也是Match对象的迭代器,获取匹配结果需要调用Match对象的group()、groups或group(index)方法。
group()、groups()与group(index)的区别,如下所示: >>> 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了。 例子 s ="1113446777"
用正则表达式把s分为1111, 3, 44, 6, 777 >>> 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 另一个例子: >>> 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

Python中re的match、search、findall、finditer区别的更多相关文章

  1. python基础-6.1 match search findall group(s) 区别

    import re # match findall经常用 # re.match() #从开头匹配,没有匹配到对象就返回NONE # re.search() #浏览全部字符,匹配第一个符合规则的字符串 ...

  2. python中dtype,type,astype的区别

    python中dtype,type,astype的区别 type() dtype() astype() 函数名称 用法 type 返回参数的数据类型 dtype 返回数组中元素的数据类型 astype ...

  3. Python基础学习-Python中最常见括号()、[]、{}的区别

    Python中最常见括号的区别: 在Python语言中最常见的括号有三种,分别是:小括号().中括号[].花括号{}:其作用也不相同,分别用来代表不同的Python基本内置数据类型. Python中的 ...

  4. 关于python中赋值、浅拷贝、深拷贝之间区别的深入分析

    当重新学习了计算机基础课程<数据结构和算法分析>后再来看这篇自己以前写的博文,发现错误百出.python内置数据类型之所以会有这些特性,归根结底是它采用的是传递内存地址的方式,而不是传递真 ...

  5. Python中function(函数)和methon(方法)的区别

    在Python中,对这两个东西有明确的规定: 函数function —— A series of statements which returns some value to a caller. It ...

  6. Python中os与sys两模块的区别

    <os和sys的官方解释> ➤os os: This module provides a portable way of using operating system dependent ...

  7. python中filter、map、reduce的区别

    python中有一些非常有趣的函数,今天也来总结一下,不过该类的网上资料也相当多,也没多少干货,只是习惯性将一些容易遗忘的功能进行整理. lambda 为关键字.filter,map,reduce为内 ...

  8. 图解python中赋值、浅拷贝、深拷贝的区别

    Python中,对象的赋值,拷贝(深/浅拷贝)之间是有差异的,如果使用的时候不注意,就可能产生意外的结果.下面本文就通过简单的例子介绍一下这些概念之间的差别. 对象赋值 直接看一段代码: will = ...

  9. python中字符编码及unicode和utf-8区别

    ascii和unicode是字符集,utf-8是编码集 字符集:为每一个「字符」分配一个唯一的 ID(学名为码位 / 码点 / Code Point) 编码规则:将「码位」转换为字节序列的规则(编码/ ...

随机推荐

  1. [SDOI2011]消耗战(虚树)

    洛古题面 题意:给定一棵树,割断每一条边都有代价,每次询问会给定一些点,求用最少的代价使所有给定点都和1号节点不连通 暴力\(DP\) 我们先考虑暴力怎么做 设\(dp[u]\)为以\(u\)为根的子 ...

  2. sqlalchemy和pymysql通过ssh连接远程mysql服务器

    首先需要一个模块sshtunnel,如果没有直接pip install sshtunnel 其实连个连接方式非常像: pymysql连接方式: import pymysql from sshtunne ...

  3. 题解-洛谷P1981 表达式求值(模拟+处理优先级的递归)

    https://www.luogu.org/problemnew/show/P1981 (原题链接) 显然乘法的优先级高与加法,因此碰到乘号就要优先把一连串与乘号相连的数算出,很容易想到递归.可用普通 ...

  4. 计算pi的位数

    from random import random from math import sqrt from time import clock DARTS=1000000 hits=0.0 clock( ...

  5. python之OpenCv(四)---人脸识别

    对特定图像进行识别,最关键的是要有识别对象的特征文件.OpenCV已经内置了人脸识别特征文件,我们只要使用OpenCV的CascadeClassifier类即可进行识别. 语法: https://gi ...

  6. Angular: 执行ng lint后如何快速修改错误

    当我第一次被分配到“修正执行ng lint语句后的错误”这项任务前,我就被导师提前告知这是一个很无聊的任务,当我开始后,我发现其实有一些办法可以加快这个无聊单调的工作.接下来,我就分享一下我的经验. ...

  7. MyBatis集成到Spring时配置MapperScannerConfigurer出错

    问题描述 在web项目中同时集成了spring mvc和mybatis. 将jdbc配置参数独立在外部配置文件中,然后通过<context:property-placeholder>引入. ...

  8. [再寄小读者之数学篇](2014-06-20 求极限-H\"older 不等式的应用)

    设非负严格增加函数 $f$ 在区间 $[a,b]$ 上连续, 有积分中值定理, 对于每个 $p>0$ 存在唯一的 $x_p\in (a,b)$, 使 $$\bex f^p(x_p)=\cfrac ...

  9. Win7 x64位打开VirtualBox报错处理。

    错误代码如下: Failed to instantiate CLSID_VirtualBox w/ IVirtualBox, but CLSID_VirtualBox w/ IUnknown work ...

  10. Nmpy函数总结

    函数和方法method总览 这是个Numpy函数和方法分类排列目录. 创建数组 arange, array, copy, empty, empty_like, eye, fromfile, fromf ...