《python核心编程》读书笔记-尊龙游戏旗舰厅官网
尊龙游戏旗舰厅官网
收集整理的这篇文章主要介绍了
《python核心编程》读书笔记--第15章 正则表达式
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
15.1引言与动机
处理文本和数据是一件大事。正则表达式(re)为高级文本匹配模式,为搜索-替换等功能提供了基础。re是由一些字符和特殊符号组成的字符串,它们描述了这些字符和字符串的某种重复方式,因此能按某种模式匹配一个有相似特征的字符串的集合,也就是说,一个只能匹配一个字符串的re是无聊的。
python通过标准库的re模块支持正则表达式。
15.2正则表达式使用的特殊符号和字符
正则表达式中常见的字符列表。包括|、.、等,这个方面已经有很多讲解。比如这篇:http://blog.csdn.net/pleasecallmewhy/article/details/8929576(引用感谢)。
15.3正则表达式和python语言
这一节将python中的re模块。主要讲match、search、compile等函数。
#-*- coding:utf-8 -*- import re#下面看一下match和group的基本用法 m = re.match('foo','foo') if m is not none:print m.group()m = re.match('foo','car') if m is not none:print m.group()m = re.match('foo','foo on the table') print m.group() print '-'*50 #下面是search和match的区别,search会从任意的地方开始搜索匹配模式 m = re.match('foo','seafood') if m is not none:print m.group() #匹配失败 m = re.search('foo','seafood') if m is not none:print m.group() #匹配成功 print '-'*50 bt = 'bat|bet|bit' m = re.match(bt,'bat') if m is not none:print m.group() m = re.match(bt,'he bit me') if m is not none:print m.group() #匹配不成功 m = re.search(bt,'he bit me') if m is not none:print m.group() #匹配成功 print '-'*50 #下面将要说明句点不能匹配换行符 anyend = '.end' m = re.match(anyend,'bend') if m is not none:print m.group() #匹配成功 m = re.match(anyend,'\nend') if m is not none:print m.group() #匹配不成功 m = re.search(anyend,'the end') if m is not none:print m.group() print '-'*50 #用转义字符表示句点 p1 = '3.14' p2 = '3\.14' print re.match(p1,'3.14').group() #成功,注意这里的.也属于‘任意字符’ print re.match(p1,'3014').group() #成功 print re.match(p2,'3.14').group() #成功 print re.match(p2,'3014').group() #出现错误>>> foo foo -------------------------------------------------- foo -------------------------------------------------- bat bit -------------------------------------------------- traceback (most recent call last):bendend -------------------------------------------------- 3.14 3014 3.14file "e:\nut\py\pycore\chapter15.py", line 45, in转载于:https://www.cnblogs.com/batteryhp/p/5188376.html
总结
以上是尊龙游戏旗舰厅官网为你收集整理的《python核心编程》读书笔记--第15章 正则表达式的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 负载均衡之我见
- 下一篇: