《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,'').group() #成功
print re.match(p2,'3.14').group() #成功
print re.match(p2,'').group() #出现错误 >>>
foo
foo
--------------------------------------------------
foo
--------------------------------------------------
bat
bit
--------------------------------------------------
Traceback (most recent call last):bend
end
--------------------------------------------------
3.14
3014
3.14 File "E:\NUT\PY\pycore\chapter15.py", line 45, in <module>
print re.match(p2,'').group()
AttributeError: 'NoneType' object has no attribute 'group'
[Finished in 0.1s with exit code 1]
#-*- coding:utf-8 -*-
import re #下面是创建字符集合 []和|的区别
m = re.match('[cr][23][dp][o2]','c3po')
print m.group() #成功匹配
print re.match('[cr][23][dp][o2]','c2do').group() #成功匹配
m = re.match('r2d2|c3po','c2do') #并不成功
if m is not None:print m.group()
print '-'*50
#重复、特殊字符和子组
#正则表达式最常见的情况包括特殊字符的使用,正则表达式模式的重复出现,以及使用圆括号对匹配模式进行分组和提取操作
patt = '\w+@(\w+\.)?\w+\.com'
print re.match(patt,'ored@xxx.com').group()
print re.match(patt,'ored@www.xxx.com').group()
print '-'*50
#下面看一下子组
m = re.match('(\w\w\w)-(\d\d\d)','abc-123')
print m.group() #group返回所有匹配的内容
print m.group(1)
print m.group(2)
print m.groups() #groups返回所有子组组成的元组
#下面用几个例子展示group和groups的不同
print '-'*50
m = re.match('ab','ab') #没有子组
print m.group() #返回完全匹配
print m.groups() #返回空
m = re.match('(ab)','ab') #这样的形式是有一个子组
print m.group()
print m.groups()
print '-'*50
m = re.match('(a)(b)','ab')
print m.group() #注意这里的机制是整体匹配
print m.groups()
m = re.match('(a(b))','ab')
print m.groups() #首先匹配外层,再匹配内层
>>>
c3po
c2do
--------------------------------------------------
ored@xxx.com
ored@www.xxx.com
--------------------------------------------------
abc-123
abc
123
('abc', '')
--------------------------------------------------
ab
()
ab
('ab',)
--------------------------------------------------
ab
('a', 'b')
('ab', 'b')
[Finished in 0.1s]
《python核心编程》读书笔记--第15章 正则表达式的更多相关文章
- C++Windows核心编程读书笔记
转自:http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E6%96%87/71405.shtml "C++Windows核心编程读书笔 ...
- python高级编程读书笔记(一)
python高级编程读书笔记(一) python 高级编程读书笔记,记录一下基础和高级用法 python2和python3兼容处理 使用sys模块使程序python2和python3兼容 import ...
- pthon核心编程-读书笔记:知识点摘录与总结(方便理解和快速记忆)
Python 中的列表(大小可变的数组)和字典(哈希表)就是内建于语言本身的.在核心语言中提供这些重要的构建单元,可以鼓励人们使用它们, 缩短开发时间与代码量,产生出可读性更好的代码.C不提供, c+ ...
- Python核心编程--学习笔记--6--序列(下)列表、元组
11 列表 类似于C语言的数组,但是列表可以包含不同类型的任意对象.列表是可变类型. 创建列表——手动赋值.工厂函数: >>> aList = [12, 'abc'] >> ...
- C++ primer plus读书笔记——第15章 友元、异常和其他
第15章 友元.异常和其他 1. 友元类的所有方法都可以访问原有类的私有成员和保护成员.另外,也可以做更严格的限制,只将特定的成员函数指定为另一个类的友元.哪些函数.成员函数.或类为友元是由类定义的, ...
- Python核心编程第三版第二章学习笔记
第二章 网络编程 1.学习笔记 2.课后习题 答案是按照自己理解和查阅资料来的,不保证正确性.如由错误欢迎指出,谢谢 1. 套接字:A network socket is an endpoint of ...
- Python核心编程--学习笔记--1--Python简介
本章介绍了Python的背景知识,包括什么是Python.Python的起源以及Python的一些关键特性. 1 什么是Python Python是一门优雅而健壮的编程语言,它继承了传统编译语言的强大 ...
- Python核心编程--学习笔记--6--序列(上)字符串
本章研究Python中的序列:字符串.列表和元组.因为这些类型其实都是由一些成员共同组成的一个序列整体,所以我们把它们统称为序列.序列的存储结构可以表示为: 1 序列 序列类型有着相同的访问模式:按下 ...
- Python核心编程--学习笔记--5--数字
本章的主题是Python中的数字,这里详细介绍每一种数字类型,它们适用的各种运算符,以及用于处理数字的内建函数.在本章的末尾简单介绍了几个标准库中用于处理数字的模块. 1 数字类型 数字:标量贮存,可 ...
随机推荐
- CPU informition
tar jxvf util-linux-ng-2.18.bz2cd util-linux-ng-2.18/./configure --enable-arch --enable-partx --enab ...
- Java初学--无限循环
利用for循环和while循环分别做到,从键盘读取任意数,输入0自动跳出无限循环,并判断有几个正数几个负数. 1.for循环的无限循环: import java.util.Scanner;//引用Sc ...
- SQL order by 两个字段排序
select * from emp;
- acm算法模板(5)
STL 中 sort 函数用法简介 做 ACM 题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的 O(n^2) 排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错. ST ...
- ofbiz进击 第六节。 --OFBiz配置之[widget.properties] 配置属性的分析
配置内容分析如下 # -- 定义上下文使用者 -- security.context =default # -- 定义密码限制长度最小值 -- password.length.min =5 # -- ...
- bzoj4137 [FJOI2015]火星商店问题
比较容易想到的做法是线段树套字典树,修改操作时在字典树上经过的节点维护一个最近被访问过的时间,这样询问操作只经过满足时间条件的节点,时间复杂度O(NlogN^2)但是因为线段树每个节点都要套个字典树, ...
- java使用json将HashMap转化成javabean小例子
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import net.sf.json.JSONOb ...
- 验证你的邮箱是不是qq邮箱
Console.WriteLine("请输入你的qq邮箱"); string yx = Console.ReadLine(); int a = yx.LastIndexOf(&qu ...
- 《科学》封面:人工智能终于能像人类一样学习 zz
原文地址:http://tech.sina.com.cn/d/i/2015-12-12/doc-ifxmpnqi6368668.shtml science Human-level concept ...
- 关于wxwidgets图形界面的关闭窗口的按钮无效的解决办法
这是使用wxsmith设计界面时的情况,如果用纯代码写的界面,关闭按钮就很奇怪地有效 道听途说,窗口的关闭是由一个方法控制着.大概是这样的: void PlainFrame::OnClose(wxCl ...