正則表達式


常用正則表達式

匹配中文字符

[\u4e00-\u9fa5]


匹配雙字節字符(包括漢字在內)

[^\x00-\xff]


匹配空白行

\n\s*\r


匹配Email地址

[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?


匹配網址URL

[a-zA-z]+://[^\s]*


匹配國內電話號碼

\d{3}-\d{8}|\d{4}-\{7,8}


匹配騰訊QQ號

[1-9][0-9]{4,}


匹配中國郵政編碼

[1-9]\d{5}(?!\d)


匹配18位身份證號

^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$


匹配(年-月-日)格式日期

([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))


匹配正整數

^[1-9]\d*$


匹配負整數

^-[1-9]\d*$


匹配整數

^-?[1-9]\d*$


匹配非負整數(正整數 + 0)

^[1-9]\d*|0$


匹配非正整數(負整數 + 0)

^-[1-9]\d*|0$


匹配正浮點數

^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$


匹配負浮點數

^-[1-9]\d*\.\d*|-0\.\d*[1-9]\d*$


幾個匹配演練

1、

#!/usr/bin/env python
# encoding: utf-8
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)
    
result = re.search('<li.*?singer="(.*?)">(.*?)</a>', html, re.S)
if result:
    print result.group(1), result.group(2)
    
result = re.search('<li.*?singer="(.*?)">(.*?)</a>', html)
if result:
    print result.group(1), result.group(2)

結果:

任賢齊 滄海一聲笑
齊秦 往事隨風
beyond 光輝歲月


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>'''
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])

結果:

[('/2.mp3', '任賢齊', '滄海一聲笑'), ('/3.mp3', '齊秦', '往事隨風'), ('/4.mp3', 'beyond', '光輝歲月'), ('/5.mp3', '陳慧琳', '記事本'), ('/6.mp3', '鄧麗君', '但願人長久')]
<class 'list'>
('/2.mp3', '任賢齊', '滄海一聲笑')
/2.mp3 任賢齊 滄海一聲笑
('/3.mp3', '齊秦', '往事隨風')
/3.mp3 齊秦 往事隨風
('/4.mp3', 'beyond', '光輝歲月')
/4.mp3 beyond 光輝歲月
('/5.mp3', '陳慧琳', '記事本')
/5.mp3 陳慧琳 記事本
('/6.mp3', '鄧麗君', '但願人長久')
/6.mp3 鄧麗君 但願人長久


3、

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>'''
html = re.sub('<a.*?>|</a>', '', html)
print(html)
results = re.findall('<li.*?>(.*?)</li>', html, re.S)
print(results)
for result in results:
    print(result.strip())

結果:

<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">
            滄海一聲笑
        </li>
        <li data-view="4" class="active">
            往事隨風
        </li>
        <li data-view="6">光輝歲月</li>
        <li data-view="5">記事本</li>
        <li data-view="5">
            但願人長久
        </li>
    </ul>
 </div>
['一路上有你', '\n            滄海一聲笑\n        ', '\n            往事隨風\n        ', '光輝歲月', '記事本', '\n            但願人長久\n        ']
一路上有你
滄海一聲笑
往事隨風
光輝歲月
記事本
但願人長久


實戰

import requests
import re
content = requests.get('https://book.douban.com/').text
pattern = re.compile('<li.*?cover.*?href="(.*?)".*?title="(.*?)".*?more-meta.*?author">(.*?)</span>.*?year">(.*?)</span>.*?</li>', re.S)
results = re.findall(pattern, content)
for result in results:
    url, name, author, date = result
    author = re.sub('\s', '', author)
    date = re.sub('\s', '', date)
    print(url, name, author, date)

結果:

https://book.douban.com/subject/26925834/?icn=index-editionrecommend 別走出這一步 [英]S.J.沃森 2017-1
https://book.douban.com/subject/26953532/?icn=index-editionrecommend 白先勇細說紅樓夢 白先勇 2017-2-1
https://book.douban.com/subject/26959159/?icn=index-editionrecommend 歲月兇猛 馮侖 2017-2
https://book.douban.com/subject/26949210/?icn=index-editionrecommend 如果沒有今天,明天會不會有昨天? [瑞士]伊夫·博薩爾特(YvesBossart) 2017-1
https://book.douban.com/subject/27001447/?icn=index-editionrecommend 人類這100年 阿夏 2017-2
https://book.douban.com/subject/26864566/?icn=index-latestbook-subject 眼淚的化學 [澳]彼得·凱里 2017-2
https://book.douban.com/subject/26991064/?icn=index-latestbook-subject 青年斯大林 [英]西蒙·蒙蒂菲奧裏 2017-3
https://book.douban.com/subject/26938056/?icn=index-latestbook-subject 帶艾伯特回家 [美]霍默·希卡姆 2017-3
https://book.douban.com/subject/26954757/?icn=index-latestbook-subject 乳房 [美]弗洛倫斯·威廉姆斯 2017-2
https://book.douban.com/subject/26956479/?icn=index-latestbook-subject 草原動物園 馬伯庸 2017-3
https://book.douban.com/subject/26956018/?icn=index-latestbook-subject 販賣音樂 [美]大衛·伊斯曼 2017-3-1
https://book.douban.com/subject/26703649/?icn=index-latestbook-subject 被佔的宅子 [阿根廷]胡利奧·科塔薩爾 2017-3
https://book.douban.com/subject/26578402/?icn=index-latestbook-subject 信仰與觀看 [法]羅蘭·雷希特(RolandRecht) 2017-2-17
https://book.douban.com/subject/26939171/?icn=index-latestbook-subject 妹妹的墳墓 [美]羅伯特·杜格尼(RobertDugoni) 2017-3-1
https://book.douban.com/subject/26972465/?icn=index-latestbook-subject 全棧市場人 Lydia 2017-2-1
https://book.douban.com/subject/26986928/?icn=index-latestbook-subject 終極X戰警2 [英]馬克·米勒 / [美]亞當·庫伯特 2017-3-15
https://book.douban.com/subject/26948144/?icn=index-latestbook-subject 格調(修訂第3版) [美]保羅·福塞爾(PaulFussell) 2017-2
https://book.douban.com/subject/26945792/?icn=index-latestbook-subject 原諒石 [美]洛裏·斯皮爾曼 2017-2
https://book.douban.com/subject/26974207/?icn=index-latestbook-subject 庇護二世聞見錄 [意]皮科洛米尼 2017-2
https://book.douban.com/subject/26983143/?icn=index-latestbook-subject 遇見野兔的那一年 [芬]阿託·帕西林納 2017-3-1
https://book.douban.com/subject/26976429/?icn=index-latestbook-subject 鮑勃·迪倫:詩人之歌 [法]讓-多米尼克·布里埃 2017-4
https://book.douban.com/subject/26962860/?icn=index-latestbook-subject 牙醫謀殺案 [英]阿加莎·克里斯蒂 2017-3
https://book.douban.com/subject/26923022/?icn=index-latestbook-subject 石揮談藝錄:把生命交給舞臺 石揮 2017-2
https://book.douban.com/subject/26897190/?icn=index-latestbook-subject 理想 [美]安·蘭德 2017-2
https://book.douban.com/subject/26985981/?icn=index-latestbook-subject 青苔不會消失 袁凌 2017-4
https://book.douban.com/subject/26984949/?icn=index-latestbook-subject 地下鐵道 [美]科爾森·懷特黑德(ColsonWhitehead) 2017-3
https://book.douban.com/subject/26944012/?icn=index-latestbook-subject 極簡進步史 [英]羅納德·賴特 2017-4-1
https://book.douban.com/subject/26969002/?icn=index-latestbook-subject 駐馬店傷心故事集 鄭在歡 2017-2
https://book.douban.com/subject/26854223/?icn=index-latestbook-subject 致薇拉 [美]弗拉基米爾·納博科夫 2017-3
https://book.douban.com/subject/26841616/?icn=index-latestbook-subject 北方檔案 [法]瑪格麗特·尤瑟納爾 2017-2
https://book.douban.com/subject/26980391/?icn=index-latestbook-subject 食帖15:便當靈感集 林江 2017-2
https://book.douban.com/subject/26958882/?icn=index-latestbook-subject 生火 [法]克里斯多夫·夏布特(ChristopheChabouté)編繪 2017-3
https://book.douban.com/subject/26989163/?icn=index-latestbook-subject 文明之光(第四冊) 吳軍 2017-3-1
https://book.douban.com/subject/26878906/?icn=index-latestbook-subject 公牛山 [美]布賴恩·帕諾威奇 2017-2
https://book.douban.com/subject/26989534/?icn=index-latestbook-subject 幾乎消失的偷閒藝術 [加拿大]達尼·拉費裏埃 2017-4
https://book.douban.com/subject/26939973/?icn=index-latestbook-subject 散步去 [日]谷口治郎 2017-3
https://book.douban.com/subject/26865333/?icn=index-latestbook-subject 中國1945 [美]理查德·伯恩斯坦(RichardBernstein) 2017-3-1
https://book.douban.com/subject/26989242/?icn=index-latestbook-subject 有匪2:離恨樓 Priest 2017-3
https://book.douban.com/subject/26985790/?icn=index-latestbook-subject 女人、火與危險事物 [美]喬治·萊考夫 2017-3
https://book.douban.com/subject/26972277/?icn=index-latestbook-subject 尋找時間的人 [愛爾蘭]凱特·湯普森 2017-3
https://www.douban.com/note/610758170/ 白先勇細說紅樓夢【全二冊】 白先勇 2017-2-1
https://read.douban.com/ebook/31540864/?dcs=book-hot&dcm=douban&dct=read-subject 奇愛博士 [英]彼得·喬治 2016-8-1
https://read.douban.com/ebook/31433872/?dcs=book-hot&dcm=douban&dct=read-subject 在時光中盛開的女子 李筱懿 2017-3
https://read.douban.com/ebook/31178635/?dcs=book-hot&dcm=douban&dct=read-subject 如何高效記憶(原書第2版) [美]肯尼思•希格比(KennethL.Higbee) 2017-3-5
https://read.douban.com/ebook/31358183/?dcs=book-hot&dcm=douban&dct=read-subject 願無歲月可回頭 回憶專用小馬甲 2016-9
https://read.douban.com/ebook/31341636/?dcs=book-hot&dcm=douban&dct=read-subject 走神的藝術與科學 [新西蘭]邁克爾·C.科爾巴里斯 2017-3-1
https://read.douban.com/ebook/27621094/?dcs=book-hot&dcm=douban&dct=read-subject 神祕的量子生命 [英]吉姆•艾爾-哈利利/約翰喬•麥克法登 2016-8
https://read.douban.com/ebook/31221966/?dcs=book-hot&dcm=douban&dct=read-subject 尋找時間的人 [愛爾蘭]凱特·湯普森 2017-3
https://read.douban.com/ebook/31481323/?dcs=book-hot&dcm=douban&dct=read-subject 山之四季 [日]高村光太郎 2017-1
https://read.douban.com/ebook/31154855/?dcs=book-hot&dcm=douban&dct=read-subject 東北遊記 [美]邁克爾·麥爾 2017-1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章