正则表达式的相关用法

来源:互联网 发布:企业网络公关 编辑:程序博客网 时间:2024/05/21 15:38

正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。
大家在写正则表达式的过程中,可利用开源中国的正则表达式测试工具,对其进行检测,链接为:http://tool.oschina.net/regex/
常见的正则表达式匹配模式如下所示:
这里写图片描述

re.match

re.match尝试从字符串的起始位置匹配的一个模式,如果不是起始位置匹配的话,match()就返回none
基本的语法结构:re.match(pattern,string,flags=0)
最常规的匹配

#_*_coding: utf-8_*_import recontent="Hello 123 456 word_This is a Regex Demo"result=re.match("^Hello\s\d+\s\d+\s\w+.*Demo$",content)print(result)print(result.group())print(result.span())

泛匹配

import recontent="hello 123 4567 world_this is a Regex Demo"result=re.match("^hello.*Demo$",content)print(result)print(result.group())print(result.span())

匹配目标

import recontent="hello 123 4567 world_this is a Regex Demo"result=re.match("^hello\s(\d+).*Demo$",content)print(result)print(result.group())print(result.group(1))print(result.span())

贪婪匹配

import recontent="hello 123 4567 world_this is a Regex Demo"result=re.match("^h.*(\d+).*Demo$",content)print(result)print(result.group(1))

非贪婪匹配

import recontent="hello 123 4567 world_this is a Regex Demo"result=re.match("^h.*?(\d+).*?Demo$",content)print(result)print(result.group(1))

指定匹配模式,.*可匹配换行符

import recontent='''hello 123 4567 world_thisis a Regex Demo'''result=re.match("^h.*?(\d+).*?Demo$",content,re.S)print(result)print(result.group(1))

转义字符

import recontent="the price is $5.00"result=re.match("the price is \$5\.00",content)print(result)

总结:尽量使用泛匹配,使用括号获取到匹配目标,尽量使用非贪婪模式,有换行符就用re.S

re.search

re.search,扫描整个字符串,并返回第一个成功的匹配
总结:为匹配方便,能用search,就不用match

import recontent='''Extra string hello 123 4567 world_thisis a Regex Demo Extra string'''result=re.search("h.*?(\d+).*?Demo",content,re.S)print(result)print(result.group(1))

匹配实例:

#_*_coding: utf-8_*_import rehtml='''<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="/3.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))

re.findall

搜索字符串,以列表形式返回所有匹配结果

#_*_coding: utf-8_*_import rehtml='''<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="/3.mp3" singer="邓丽君"><i class="fa fa-user"></i>但愿人长久</a>    </li>    </ul><div>'''results=re.findall('<li.*?singer="(.*?)">(.*?)</a>',html,re.S)if results:    for result in results:        print(result)

re.sub

替换字符串中每一个匹配的子串返回替换后的字符

#_*_coding: utf-8_*_import recontent="Extra strings Hello 1234567 World_This is a Regex Demo Extra strings"content=re.sub('\d+','',content)print(content)

如果替换的字符串包含元字符串本身,可采用下面的方法:

#_*_coding: utf-8_*_import recontent="Extra strings Hello 1234567 World_This is a Regex Demo Extra strings"content=re.sub('(\d+)',r'\1 8910',content)print(content)

实例:去除HTML代码中的a标签,并获取歌名

#_*_coding: utf-8_*_import rehtml='''<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="/3.mp3" singer="邓丽君"><i class="fa fa-user"></i>但愿人长久</a>    </li>    </ul><div>'''html=re.sub('<a.*?>|</a>','',html)html=re.sub('<i.*?></i>','',html)print(html)results=re.findall('<li.*?>(.*?)</li>',html,re.S)print("------------------------------------")for result in results:    print(result.strip())

re.compile

将正则表达式字符串编译成正则表达式对象

#_*_coding: utf-8_*_import recontent='''Hello 1234567 World_Thisis a Regex Demo'''pattern=re.compile('Hello.*?Demo',re.S)result=re.match(pattern,content)print(result)

实战,爬取豆瓣的信息

#_*_coding: utf-8_*_import reimport requestscontent=requests.get("https://book.douban.com").textpattern=re.compile('<li.*?cover.*?href="(.*?)".*?title="(.*?)".*?author.*?>(.*?)</div>.*?</li>',re.S)content=re.sub('&nbsp;','',content)results=re.findall(pattern,content)for result in results:    print("url=",result[0].strip())    print("作者是", result[2].strip())    print("书名是", result[1].strip())

运行结果如下:
这里写图片描述

原创粉丝点击