Python正则表达式与re模块
2018-01-12 01:52
417 查看
在线正则表达式测试
http://tool.oschina.net/regex/
常见匹配模式
模式 | 描述 |
---|---|
\w | 匹配字母数字及下划线 |
\W | 匹配非字母数字下划线 |
\s | 匹配任意空白字符,等价于 [\t\n\r\f]. |
\S | 匹配任意非空字符 |
\d | 匹配任意数字,等价于 [0-9] |
\D | 匹配任意非数字 |
\A | 匹配字符串开始 |
\Z | 匹配字符串结束,如果是存在换行,只匹配到换行前的结束字符串 |
\z | 匹配字符串结束 |
\G | 匹配最后匹配完成的位置 |
\n | 匹配一个换行符 |
\t | 匹配一个制表符 |
^ | 匹配字符串的开头 |
$ | 匹配字符串的末尾。 |
. | 匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符。 |
[...] | 用来表示一组字符,单独列出:[amk] 匹配 'a','m'或'k' |
[^...] | 不在[]中的字符:[^abc] 匹配除了a,b,c之外的字符。 |
* | 匹配0个或多个的表达式。 |
+ | 匹配1个或多个的表达式。 |
? | 匹配0个或1个由前面的正则表达式定义的片段,非贪婪方式 |
{n} | 精确匹配n个前面表达式。 |
{n, m} | 匹配 n 到 m 次由前面的正则表达式定义的片段,贪婪方式 |
a|b | 匹配a或b |
( ) | 匹配括号内的表达式,也表示一个组 |
re.match
re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none。
re.match(pattern, string, flags=0)
最常规的匹配
import re content = 'Hello 123 4567 World_This is a Regex Demo' print(len(content)) result = re.match('^Hello\s\d\d\d\s\d{4}\s\w{10}.*Demo$', content) print(result) print(result.group()) print(result.span())
41 <_sre.SRE_Match object; span=(0, 41), match='Hello 123 4567 World_This is a Regex Demo'> Hello 123 4567 World_This is a Regex Demo (0, 41)
泛匹配
import re content = 'Hello 123 4567 World_This is a Regex Demo' result = re.match('^Hello.*Demo$', content) print(result) print(result.group()) print(result.span())
<_sre.SRE_Match object; span=(0, 41), match='Hello 123 4567 World_This is a Regex Demo'> Hello 123 4567 World_This is a Regex Demo (0, 41)
匹配目标
import re content = 'Hello 1234567 World_This is a Regex Demo' result = re.match('^Hello\s(\d+)\sWorld.*Demo$', content) print(result) print(result.group(1)) print(result.span())
<_sre.SRE_Match object; span=(0, 40), match='Hello 1234567 World_This is a Regex Demo'> 1234567 (0, 40)
贪婪匹配
import re content = 'Hello 1234567 World_This is a Regex Demo' result = re.match('^He.*(\d+).*Demo$', content) print(result) print(result.group(1))
<_sre.SRE_Match object; span=(0, 40), match='Hello 1234567 World_This is a Regex Demo'> 7
非贪婪匹配
import re content = 'Hello 1234567 World_This is a Regex Demo' result = re.match('^He.*?(\d+).*Demo$', content) print(result) print(result.group(1))
<_sre.SRE_Match object; span=(0, 40), match='Hello 1234567 World_This is a Regex Demo'> 1234567
匹配模式
import re content = '''Hello 1234567 World_This is a Regex Demo ''' result = re.match('^He.*?(\d+).*?Demo$', content, re.S) print(result.group(1))
1234567
转义
import re content = 'price is $5.00' result = re.match('price is $5.00', content) print(result)
None
import re content = 'price is $5.00' result = re.match('price is \$5\.00', content) print(result)
<_sre.SRE_Match object; span=(0, 14), match='price is $5.00'>
总结:尽量使用泛匹配、使用括号得到匹配目标、尽量使用非贪婪模式、有换行符就用re.S
re.search
re.search 扫描整个字符串并返回第一个成功的匹配。
import re content = 'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings' result = re.match('Hello.*?(\d+).*?Demo', content) print(result)
None
import re content = 'Extra stings Hello 1234567 World_This is a Regex Demo Extra stings' result = re.search('Hello.*?(\d+).*?Demo', content) print(result) print(result.group(1))
<_sre.SRE_Match object; span=(13, 53), match='Hello 1234567 World_This is a Regex Demo'> 1234567
总结:为匹配方便,能用search就不用match
匹配演练
import re html = '''<div id="songs-list"> <h2 class="title">经典老歌</h2> <p class="introduction"> 经典老歌列表 </p> <ul id="list" class="list-group"> <li data-view="2">一路上有你</li> <li data-view="7"> <a href="/2.mp3" singer="任贤齐">沧海一声笑</a> </li> <li data-view="4" class="active"> <a href="/3.mp3" singer="齐秦">往事随风</a> </li> <li data-view="6"><a href="/4.mp3" singer="beyond">光辉岁月</a></li> <li data-view="5"><a href="/5.mp3" singer="陈慧琳">记事本</a></li> <li data-view="5"> <a href="/6.mp3" singer="邓丽君"><i class="fa fa-user"></i>但愿人长久</a> </li> </ul> </div>''' result = re.search('<li.*?active.*?singer="(.*?)">(.*?)</a>', html, re.S) if result: print(result.group(1), result.group(2))
齐秦 往事随风
import re html = '''<div id="songs-list"> <h2 class="title">经典老歌</h2> <p class="introduction"> 经典老歌列表 </p> <ul id="list" class="list-group"> <li data-view="2">一路上有你</li> <li data-view="7"> <a href="/2.mp3" singer="任贤齐">沧海一声笑</a> </li> <li data-view="4" class="active"> <a href="/3.mp3" singer="齐秦">往事随风</a> </li> <li data-view="6"><a href="/4.mp3" singer="beyond">光辉岁月</a></li> <li data-view="5"><a href="/5.mp3" singer="陈慧琳">记事本</a></li> <li data-view="5"> <a href="/6.mp3" singer="邓丽君">但愿人长久</a> </li> </ul> </div>''' result = re.search('<li.*?singer="(.*?)">(.*?)</a>', html, re.S) if result: print(result.group(1), result.group(2))
任贤齐 沧海一声笑
import re html = '''<div id="songs-list"> <h2 class="title">经典老歌</h2> <p class="introduction"> 经典老歌列表 </p> <ul id="list" class="list-group"> <li data-view="2">一路上有你</li> <li data-view="7"> <a href="/2.mp3" singer="任贤齐">沧海一声笑</a> </li> <li data-view="4" class="active"> <a href="/3.mp3" singer="齐秦">往事随风</a> </li> <li data-view="6"><a href="/4.mp3" singer="beyond">光辉岁月</a></li> <li data-view="5"><a href="/5.mp3" singer="陈慧琳">记事本</a></li> <li data-view="5"> <a href="/6.mp3" singer="邓丽君">但愿人长久</a> </li> </ul> </div>''' result = re.search('<li.*?singer="(.*?)">(.*?)</a>', html) if result: print(result.group(1), result.group(2))
beyond 光辉岁月
re.findall
搜索字符串,以列表形式返回全部能匹配的子串。
import re html = '''<div id="songs-list"> <h2 class="title">经典老歌</h2> <p class="introduction"> 经典老歌列表 </p> <ul id="list" class="list-group"> <li data-view="2">一路上有你</li> <li data-view="7"> <a href="/2.mp3" singer="任贤齐">沧海一声笑</a> </li> <li data-view="4" class="active"> <a href="/3.mp3" singer="齐秦">往事随风</a> </li> <li data-view="6"><a href="/4.mp3" singer="beyond">光辉岁月</a></li> <li data-view="5"><a href="/5.mp3" singer="陈慧琳">记事本</a></li> <li data-view="5"> <a href="/6.mp3" singer="邓丽君">但愿人长久</a> </li> </ul> </div>''' results = re.findall('<li.*?href="(.*?)".*?singer="(.*?)">(.*?)</a>', html, re.S) print(results) print(type(results)) for result in results: print(result) print(result[0], result[1], result[2])
https://book.douban.com/subject/27113800/?icn=index-editionrecommend 风起陇西 马伯庸 2017-10 https://book.douban.com/subject/27609047/?icn=index-editionrecommend 偶遇 陈鲁豫 2018-1 https://book.douban.com/subject/27174024/?icn=index-editionrecommend 混乱 [英]蒂姆·哈福德 2018-1-1 https://book.douban.com/subject/27599891/?icn=index-editionrecommend 老子之道 白云先生 2018-1 https://book.douban.com/subject/27158395/?icn=index-editionrecommend 中国的街市 [日]安野光雅 2017-10 https://book.douban.com/subject/27603589/?icn=index-latestbook-subject 郊游 荞麦 2018-1 https://book.douban.com/subject/27086102/?icn=index-latestbook-subject 国王的两个身体 [德]恩斯特·康托洛维茨 2018-1-1 https://book.douban.com/subject/27116728/?icn=index-latestbook-subject 人生拼图版 [法]乔治·佩雷克 2017-12 https://book.douban.com/subject/27114418/?icn=index-latestbook-subject 活下去的理由 [英]马特·海格 2018-1 https://book.douban.com/subject/27614904/?icn=index-latestbook-subject 房思琪的初恋乐园 林奕含 2018-1 https://book.douban.com/subject/27600198/?icn=index-latestbook-subject 贾想II 贾樟柯著 / 万佳欢编 2018-1 https://book.douban.com/subject/27603501/?icn=index-latestbook-subject 奇迹男孩 [美]R.J.帕拉西奥 2018-1 https://book.douban.com/subject/27061706/?icn=index-latestbook-subject 哈布斯堡王朝 [英]卫克安 2017-12-20 https://book.douban.com/subject/27198977/?icn=index-latestbook-subject 日本神话故事与传说 [英]格雷丝·詹姆斯 2017-12 https://book.douban.com/subject/27190980/?icn=index-latestbook-subject 晚清稀见中外关系史料丛书 苏精 2017-10-1 https://book.douban.com/subject/26916012/?icn=index-latestbook-subject 黑暗的左手 [美]厄休拉·勒古恩 2017-12 https://book.douban.com/subject/27187132/?icn=index-latestbook-subject 谷歌时代的柏拉图 [美]丽贝卡·戈尔茨坦 2017-12-10 https://book.douban.com/subject/27099392/?icn=index-latestbook-subject 越狱者 [澳]迈克尔·罗伯森 2017-12 https://book.douban.com/subject/27608239/?icn=index-latestbook-subject 原则 [美]瑞·达利欧 2018-1 https://book.douban.com/subject/27079433/?icn=index-latestbook-subject 纳博科夫短篇小说全集 [美]弗拉基米尔·纳博科夫 2018-1-1 https://book.douban.com/subject/27104087/?icn=index-latestbook-subject 谜一般的七支刀 [日]宫崎市定 2018-1 https://book.douban.com/subject/27179303/?icn=index-latestbook-subject 伊斯坦布尔假期 [法]马克·李维 2017-12-1 https://book.douban.com/subject/27086018/?icn=index-latestbook-subject 南京1937 [丹麦]何铭生 2017-8-1 https://book.douban.com/subject/27175508/?icn=index-latestbook-subject 第一个坏人 [美]米兰达·裘丽 2018-1 https://book.douban.com/subject/27176195/?icn=index-latestbook-subject 日本电影110年 [日]四方田犬彦 2018-1 https://book.douban.com/subject/27592929/?icn=index-latestbook-subject 克苏鲁神话Ⅱ [美]H.P.洛夫克拉夫特 2018-1 https://book.douban.com/subject/27136549/?icn=index-latestbook-subject 思想史:从火到弗洛伊德 [英]彼得·沃森(PeterWatson) 2018-1 https://book.douban.com/subject/27252756/?icn=index-latestbook-subject 不吃鸡蛋的人 钱佳楠 2018-1 https://book.douban.com/subject/27168433/?icn=index-latestbook-subject 基因传 [美]悉达多·穆克吉 2017-12 https://book.douban.com/subject/27194696/?icn=index-latestbook-subject 文森特与凡·高:星空中的月亮 [塞尔维亚]格拉迪米尔·斯穆贾编绘 2017-12 https://book.douban.com/subject/27174130/?icn=index-latestbook-subject 北野武的小酒馆 [日]北野武 2018-1 https://book.douban.com/subject/27200261/?icn=index-latestbook-subject 假面山庄 [日]东野圭吾 2018-1 https://book.douban.com/subject/27176511/?icn=index-latestbook-subject 编剧有章法 [美]迈克尔·豪格 2017-11 https://book.douban.com/subject/27173719/?icn=index-latestbook-subject 十一月的此刻 [美]约瑟芬·约翰逊 2018-1 https://book.douban.com/subject/27109151/?icn=index-latestbook-subject 希尼三十年文选 [爱尔兰]谢默斯·希尼 2017-12 https://book.douban.com/subject/27601742/?icn=index-latestbook-subject 50:伟大的短篇小说们 欧·亨利 / 契诃夫 / 都德 / 蒲宁 / 舒尔茨 / 波德莱尔 / 莫泊桑 / 马克·吐温 / 泰戈尔 / 海明威 / 福克纳 / 普希金 / 托尔斯泰 / 陀思妥耶夫斯基 / 爱伦·坡 / 伍尔夫 / 霍桑 / 毛姆 / 卡夫卡 / 茨威格 / 狄更斯 / 芥川龙之介 / 菲茨杰拉德 / 洛夫克拉夫特 2017-12 https://book.douban.com/subject/27110417/?icn=index-latestbook-subject 梁漱溟往来书信集 梁培宽编注 2017-11 https://book.douban.com/subject/27604190/?icn=index-latestbook-subject 我很好啊你怎么样 [美]莎拉·安徒生著绘 2018-1 https://book.douban.com/subject/27199032/?icn=index-latestbook-subject 野兽派太太 [英]卡罗尔·安·达菲 2017-1-5 https://book.douban.com/subject/27147066/?icn=index-latestbook-subject 孤独梦想家 [英]戴维·巴尼特 2017-12 https://book.douban.com/review/9039910/ 偶遇 陈鲁豫 2018-1 https://read.douban.com/ebook/33982124/?dcs=book-hot&dcm=douban&dct=read-subject 节日万岁 [日]高木直子 2017-3-31 https://read.douban.com/ebook/44418998/?dcs=book-hot&dcm=douban&dct=read-subject 克里斯汀 [美]斯蒂芬·金 2017-10 https://read.douban.com/ebook/44587068/?dcs=book-hot&dcm=douban&dct=read-subject 激荡十年,水大鱼大 吴晓波 2017-11-1 https://read.douban.com/ebook/44928867/?dcs=book-hot&dcm=douban&dct=read-subject 慢慢来,反正也来不及 囧叔 2017-12-31 https://read.douban.com/ebook/44465808/?dcs=book-hot&dcm=douban&dct=read-subject 使女的故事 [加拿大]玛格丽特·阿特伍德 2017-12 https://read.douban.com/ebook/44417760/?dcs=book-hot&dcm=douban&dct=read-subject 不烦不累扫一屋 〔日〕新津春子 2017-3-1 https://read.douban.com/ebook/43571305/?dcs=book-hot&dcm=douban&dct=read-subject 春灯公子 张大春 2017-12-15 https://read.douban.com/ebook/2424667/?dcs=book-hot&dcm=douban&dct=read-subject 心理罪:城市之光 雷米 2012-12 https://read.douban.com/ebook/34760338/?dcs=book-hot&dcm=douban&dct=read-subject 不成问题的问题 老舍 2017-5output
相关文章推荐
- Python中re(正则表达式)模块学习
- python的正则表达式re模块的常用方法
- python学习笔记正则表达式re模块
- Python 正则表达式 re 模块简明笔记
- Python用re(正则表达式)模块过滤Telnet字体颜色
- Python模块学习 re 正则表达式
- python模块之re正则表达式
- 正则表达式:Python 模块 re 简介
- Python中re(正则表达式)模块学习
- 简单学正则表达式re模块-python
- python3爬虫(二) re模块与正则表达式
- Python 正则表达式_re模块_使用compile加速
- Python正则表达式模块(re模块)
- Python中re(正则表达式)模块详解
- Python的re模块正则表达式操作
- Python中的re模块和正则表达式
- Python 正则表达式 re模块
- Python中re(正则表达式)模块
- Python中re(正则表达式)模块函数学习
- python爬虫主要就是五个模块:爬虫启动入口模块,URL管理器存放已经爬虫的URL和待爬虫URL列表,html下载器,html解析器,html输出器 同时可以掌握到urllib2的使用、bs4(BeautifulSoup)页面解析器、re正则表达式、urlparse、python基础知识回顾(set集合操作)等相关内容。