欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 编程语言 > python >内容正文

python

《python核心编程》读书笔记-尊龙游戏旗舰厅官网

发布时间:2025/1/21 python 21 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 《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 print re.match(p2,'3014').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', '123') -------------------------------------------------- ab () ab ('ab',) -------------------------------------------------- ab ('a', 'b') ('ab', 'b') [finished in 0.1s]

转载于:https://www.cnblogs.com/batteryhp/p/5188376.html

总结

以上是尊龙游戏旗舰厅官网为你收集整理的《python核心编程》读书笔记--第15章 正则表达式的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

网站地图