Python正则表达式教程_第1页
Python正则表达式教程_第2页
Python正则表达式教程_第3页
Python正则表达式教程_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、import re1查找第一个匹配用s = 'i love python very much' pat = 'python'r = re.search(pat,s) print(r.span() #(7,13)2查找所有1s =''pat = '1'r = re.finditer(pat,s)for i in r: print(i)# <re.Match object; span=(9, 10), match='1'># <re.Match object; span=(14, 15), match

2、='1'>3 d匹配数字0-9s ='一共20行代码运行时间13.59s'pat = r'd+' # +表示匹配数字(d表示数字的通用字符)1次或多次r = re.findall(pat,s)print(r)#'20', '13', '59'我们想保留13. 59而不是分开,请看44 ?表示前一个字符匹配0或1次s ='一共20行代码运行时间13.59s'pat = r'd+.?d+' #“示匹配小数点(.)0次或1次 r = re.findall(pat,s)

3、print(r)#'20', '13.59'5 A匹配字符串的开头s = 'This module provides regular expression matching operations similar to those fou nd in Perl'pat = r'Aemrt' # 查找以r = re.findall(pat,s)print(r)# ,因为字符串的开头是字符',不在emrt匹配范围内,所以返回为空 6 re.I忽略大小写 s = 'This module provides regular

4、expression matching operations similar to those fou nd in Perl'pat = r'Aemrt' # 查找以r = pile(pat,re.I).search(s)print(r)# <re.Match object; span=(0, 1), match='T'>表明字符串的开头在匹配列表中7使用正则提取单词这是不准确版本,请参看第9个s = 'This module provides regular expression matching operations simila

5、r to those fou nd in Perl'pat = r'sa-zA-Z+'r = re.findall(pat,s)print(r) #' module', ' provides', ' regular', ' expression', ' matching', ' operations', ' simil ar', ' to', ' those', ' found', ' in',

6、' Perl'8只捕获单词,去掉空格使用()捕获,这是不准确版本,请参看第9个s = 'This module provides regular expression matching operations similar to those fou nd in Perl'pat = r's(a-zA-Z+)'r = re.findall(pat,s)print(r) #'module', 'provides', 'regular', 'expression', 'matchi

7、ng', 'operations', 'similar',' to', 'those', 'found', 'in', 'Perl'9补充上第一个单词上面第8,看到提取单词中未包括第一个单词,使用?表示前面字符出现0次或1次,但是此字符还有表示贪心或非贪心匹配含义,使用时 要谨慎。s = 'This module provides regular expression matching operations similar to those fou nd in Per

8、l'pat = r's?(a-zA-Z+)'r = re.findall(pat,s)print(r) #'This', 'module', 'provides', 'regular', 'expression', 'matching', 'operations', 'si milar', 'to', 'those', 'found', 'in', 'Perl'10

9、使用spl it函数直接分割单词使用以上方法分割单词,不是简洁的,仅仅为了演示。分割单词最简 单还是使用spl it函数。s = 'This module provides regular expression matching operations similar to those fou nd in Perl'pat = r's+'r = re.split(pat,s)print(r) # 'This', 'module', 'provides', 'regular', 'express

10、ion', 'matching', 'operations', 'si milar', 'to', 'those', 'found', 'in', 'Perl'11提取以m或t开头的单词,忽略大小写 下面出现的结果不是我们想要的,原因出在?上! s = 'This module provides regular expression matching operations similar to those fou nd in Perl'pat

11、= r's?(mta-zA-Z*)' # 查找以 r = re.findall(pat,s)print(r) # 'module', 'matching', 'tions', 'milar', 'to', 'those' 12使用A查找字符串开头的单词综合11和12得到所有以m或t开头的单词s = 'This module provides regular expression matching operations similar to those fou nd in Pe

12、rl'pat = r'A(mta-zA-Z*)s' # 查找以 r = pile(pat,re.I).findall(s) print(r) # 'This'13先分割,再查找满足要求的单词 使用match表示是否匹配s = 'This module provides regular expression matching operations similar to those fou nd in Perl'pat = r's+' r = re.split(pat,s)res = i for i in r if re.ma

13、tch(r'mMtT,i)print(res) # 'This', 'module', 'matching', 'to', 'those' 14贪心匹配尽可能多的匹配字符content='<h>ddedadsad</h><div>graph</div>bb<div>math</div>cc' pat=pile(r"<div>(.*)</div>") # 贪婪模式m=pat.fi

14、ndall(content)print(m) # 'graph</div>bb<div>math'1 5非贪心匹配与14相比,仅仅多了 一个问号(?),得到结果完全不同。content='<h>ddedadsad</h><div>graph</div>bb<div>math</div>cc' pat=pile(r"<div>(.*?)</div>") # 贪婪模式m=pat.findall(content) print(m)

15、 # 'graph', 'math'与14比较可知,贪心匹配和非贪心匹配的区别,后者是字符串匹配后 立即返回,见好就收。16含有多种分割符使用spl it函数content = 'graph math,english;chemistry'钻种 pat=pile(r"s,;+") # 贪婪模式m=pat.split(content)print(m) # 'graph', 'math', 'english', 'chemistry' 17替换匹配的子用sub函数实现对匹配子用的替换content="hello 12345, hello 456321"pat=pile(r'd+')艘替换的部分m=pat.sub("666",content)print(m) # hello 666, hello 66619常用元

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论