Python学习(正则表达式与模式匹配上篇)
来源:互联网 发布:电子表格办公软件2007 编辑:程序博客网 时间:2024/06/05 16:56
一、用正则查找文本中相应的内容:
>>> import re>>> phoneNumRegex = re.compile(r'\d\d\d \d\d\d\d \d\d\d')>>> num = phoneNumRegex.search('我的电话是:010 1111 1111')>>> print(num)<_sre.SRE_Match object; span=(6, 18), match='010 1111 111'>>>> print(num.group())010 1111 1111.import re导入正则表达式模块。Python中所有正则表达式的函数都在re模块中,所以我们要先引入re模块;
2.用re.compile()函数创建一个Regex对象(参数就是要匹配的内容的正则表达式);
3.用Regex对象的search()方法来查找一段字符串,返回那个匹配的对象num,num中是一段相应的描述信息;
4.调用匹配对象num的group()方法,返回实际匹配文本的字符串。
当然,也可以使用()给查找的内容分组:
>>> phoneNumRegex = re.compile(r'(\d\d\d) (\d\d\d\d \d\d\d)')>>> num = phoneNumRegex.search('my number is 010 1111 1111')>>> num.group(1)'010'>>> num.group(2)'1111 111'>>> num.groups()('010', '1111 111')>>> num.group(0)'010 1111 111'>>> num.group()'010 1111 111'
二、用管道匹配多个分组:管道是或者的关系。
>>> name = re.compile(r'Lee|Tony')>>> names = re.compile(r'Lee|Tony')>>> name = names.search('Lee and Tony')>>> name.group()'Lee'如果 Lee和Tony都出现在被查找的字符串中,则第一次出现的匹配文本会被作为匹配对象返回。
三、用问号实现可选性的匹配:
>>> phoneNumRegex = re.compile(r'(\d\d\d)? (\d\d\d\d \d\d\d)')>>> num = phoneNumRegex.search('my number is 1111 1111')>>> num.group()' 1111 111'>>> num = phoneNumRegex.search('我的电话是:010 1111 1111')>>> num.group()'010 1111 111'四、用星号匹配零次或多次,用加号匹配一次或多次:用花括号匹配特定次数
>>> names = re.compile(r'(Lee)*')>>> name = names.search('LeeLee')>>> name.group()'LeeLee'>>> names = re.compile(r'(Lee)+')>>> name = names.search('Tony')>>> name.group()Traceback (most recent call last): File "<pyshell#34>", line 1, in <module> name.group()AttributeError: 'NoneType' object has no attribute 'group'>>> names = re.compile(r'(Lee){3}')>>> name = names.search('LeeLeeLee')>>> name.group()'LeeLeeLee'>>> name = names.search('LeeLee')>>> name.group()Traceback (most recent call last): File "<pyshell#39>", line 1, in <module> name.group()AttributeError: 'NoneType' object has no attribute 'group'五、用花括号匹配特定次数:
>>> names = re.compile(r'(Lee){3}')>>> name = names.search('LeeLeeLee')>>> name.group()'LeeLeeLee'>>> name = names.search('LeeLee')>>> name.group()Traceback (most recent call last): File "<pyshell#39>", line 1, in <module> name.group()AttributeError: 'NoneType' object has no attribute 'group'参数除了一个数字,还可以设定范围,即在花括号中写一个最小值、逗号、一个最大值。例如(Lee){1,3},将匹配'Lee'、'LeeLee'和'LeeLeeLee'三种情况。也可以不写花括号中的第一个或第二个数字,这样的话将不限定最小值或最大值。
六、贪心和非贪心匹配:默认来讲是贪心的,就是取最多的那个,如果想取遇到的第一个,在{}后加上一个?来实现。
>>> names = re.compile(r'(Lee){1,3}')>>> name = names.search('LeeLeeLee')>>> name.group()'LeeLeeLee'>>> names = re.compile(r'(Lee){1,3}?')>>> name = names.search('LeeLeeLee')>>> name.group()'Lee'七、findall()方法:
>>> phoneNumRegex = re.compile(r'\d\d\d-\d\d\d\d-\d\d\d')>>> phoneNum = phoneNumRegex.search('我的电话是:010-1111-1111和010-8888-8888')>>> phoneNum.group()'010-1111-111'>>> phoneNum = phoneNumRegex.findall('我的电话是:010-1111-1111和010-8888-8888')>>> phoneNum['010-1111-111', '010-8888-888']>>> phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d\d)-(\d\d\d)')>>> phoneNumRegex.findall('我的电话是:010-1111-1111和010-8888-8888')[('010', '1111', '111'), ('010', '8888', '888')]
1.如果在一个没有分组的正则表达式中使用,例如\d\d\d-\d\d\d\d-\d\d\d\d,findall()将返回一个其匹配的字符串的列表;
2.如果在一个有分组的正则表达式中使用,例如(\d\d\d)-(\d\d\d\d)-(\d\d\d\d),findall()将返回一个字符串的元组的列表(每个分组对应一个字符串)。
八、字符分类:
\d:0到9的任何数字;\D:除0到9的数字以外的任何字符;
\w:任何字母、数字或下划线字符(可以认为是匹配“单词”字符);\W:除字母、数字和下划线以外的任何字符;
\s:空格、制表符或换行符(可以认为是匹配“空白”字符);\S:除空格、制表符和换行符以外的任何字符。
这些分类有点笼统,根据具体的需求,你可以自己定义正则:
1.可以使用短横线表示字母或数字的范围:例如[a-zA-Z0-9]可以匹配所有小写字母、大写字母和数字。注意,在方括号内,普通的正则表达式符号不会被解释。
2.在[a-zA-Z0-9]的左上角插入字符(^),[^a-zA-Z0-9]就表示非判断,就是取除了这个要求之外的值。
九、插入字符和美元字符:
可以在正则表达式的开始处使用插入符号(^),表明匹配必须发生在被查找文本开始处。类似地,可以在正则表达式的末尾加上美元符号($),表示该字符串必须以这个正则表达式的模式结束。同时使用^和$,表明整个字符串必须匹配该正则表达式。
>>> nums = re.compile(r'^\d+$')>>> num = nums.search('a2017')>>> num.group()Traceback (most recent call last): File "<pyshell#77>", line 1, in <module> num.group()AttributeError: 'NoneType' object has no attribute 'group'>>> nums = re.compile(r'^\d+$')>>> num = nums.search('2017a')>>> num.group()Traceback (most recent call last): File "<pyshell#80>", line 1, in <module> num.group()AttributeError: 'NoneType' object has no attribute 'group'>>> nums = re.compile(r'^\d+$')>>> num = nums.search('2017')>>> num.group()'2017'
- Python学习(正则表达式与模式匹配上篇)
- Python学习(正则表达式与模式匹配下篇)
- [ Java学习 ] 正则表达式与模式匹配
- Python 正则表达式(模式匹配)
- Python 正则表达式(模式匹配)
- Python 正则表达式(模式匹配)
- Python 正则表达式(模式匹配)
- Python 3爬虫网易云(四)—— 正则表达式上篇之匹配法则
- 正则表达式与模式匹配
- 正则表达式(上篇)
- 正则表达式(上篇)
- python中的正则表达式(模式匹配)
- 常用正则表达式模式匹配学习笔记
- JavaScript中的模式匹配与正则表达式
- Bash Shell-正则表达式与模式匹配
- Bash Shell-正则表达式与模式匹配
- 正则表达式之量词与匹配模式
- python 字符串匹配与正则表达式
- Gson工具类
- readis 内部数据结构
- GSD文件简介
- 转化日期(yyyy-MM-dd)工具类
- mac 用终端修改host
- Python学习(正则表达式与模式匹配上篇)
- spring quartz 配置
- swiper轮播图插件http://www.swiper.com.cn/api/pagination/2014/1217/68.html
- Java容器---遍历List的四种方式
- 阿里云上安装svn服务
- 全栈工程师之路-中级篇之小程序开发-第二章第五节小程序中的Promise
- 谁都忍不了烂代码,如何用重构的方式让它整洁起来?
- javaweb学习总结(五)——Servlet开发(一)
- E