《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 数字类型 数字:标量贮存,可 ...
随机推荐
- 链表操作,空间复杂度要求为O(1)
对于O(1)的空间复杂度要求,不能对链表进行复制等操作,双指针法对处理该类问题比较有效. 同时由于链表头结点的特殊性,可以考虑引入一个空的头结点来辅助操作.
- sql server常见服务
根据您决定安装的组件,SQL Server 安装程序将安装以下服务: SQL Server Database Services - 用于 SQL Server 关系数据库引擎的服务. 可执行文件为 & ...
- PostgreSQL 中日期类型转换与变量使用及相关问题
PostgreSQL中日期类型与字符串类型的转换方法 示例如下: postgres=# select current_date; date ------------ 2015-08-31 (1 row ...
- sdutoj 2603 Rescue The Princess
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2603 Rescue The Princess ...
- Java基础(8):方法重载的4个依据与例子
判断方法重载的依据: 1. 必须是在同一个类中 2. 方法名相同 3. 方法参数的个数.顺序或类型不同 4. 与方法的修饰符或返回值没有关系 运行结果:
- paper 75:使用MATLAB的神经网络工具箱创建神经网络
% 生成训练样本集 clear all; clc; P=[110 0.807 240 0.2 15 1 18 2 1.5; 110 2.865 240 0.1 15 2 12 1 2; 110 2.5 ...
- linux中使用软链接时出现 too many levels of symbolic links
刚开始使用的源文件的路径是相对路径,所以导致标题中的这种错误. 只要用绝对路径表示源文件就好了.如果用相对路径的话,实际相对的是目标文件所在的路径,而在创建链接文件时用的路径是相对于当前的路径.
- 让未激活的win8.1不再跳出提示激活的窗口
以管理员运行命令行: 输入以下命令: slmgr.vbs -upk
- access调用联系
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; us ...
- jquery 下拉菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...