基於python的-正則中的函數

# -*- coding:utf-8 -*-

import re

# 1. match()
# 2. search()

string = 'hell8oworldhe8llo,h4e,hell,h6ool'
pattern = re.compile('h.*?l')
# -----------------------------------------------------------------
# findall() 最終返回的一個列表,列表中是符合正則條件的所有結果
res = re.findall(pattern, string)
print(res)

# -----------------------------------------------------------------

# split() 根據正則將字符串進行分割,返回的是存放分割後字符串的列表
pattern = re.compile('\d+')
res = re.split(pattern, string)
print(res)
# -----------------------------------------------------------------

# sub() 函數 用於替換目標字符中符合正則的字符,默認替換所有
pattern = re.compile('\d+')
# sub(正則, 替換後的字符串, 大字符串, 替換次數)  4個參數
res = re.sub(pattern, '+', string, 2)
print(res)

string3 = '<p><h1>社會主義核心價值觀<img src="http://www.baidu.com"></h1></p>'
pattern = re.compile('<.*?>')
res = re.sub(pattern, ' ', string3)
print(res)
# -----------------------------------------------------------------

string4 = '''<ul><li><a href='/html/tv/hytv/110680.html' title="2017年大陸國產劇《鳳囚凰》連載至28">2017年大陸國產劇《鳳囚凰》連載至28</a><span><font color=#FF0000>03-05</font></span></li><li><a href='/html/tv/hytv/110723.html' title="2017年香港港臺劇《無間道2018(粵語)》連載至16">2017年香港港臺劇《無間道2018(粵語)》連載至16</a><span><font color=#FF0000>03-05</font></span></li><li><a href='/html/tv/hytv/110821.html' title="2017年香港港臺劇《波士早晨(國語)》連載至15">2017年香港港臺劇《波士早晨(國語)》連載至15</a><span><font color=#FF0000>03-05</font></span></li><li><a href='/html/tv/hytv/110825.html' title="2017年香港港臺劇《波士早晨(粵語)》連載至43">2017年香港港臺劇《波士早晨(粵語)》連載至43</a><span><font color=#FF0000>03-05</font></span></li><li><a href='/html/tv/hytv/110857.html' title="2017年大陸國產劇《我的父親我的兵》連載至22">2017年大陸國產劇《我的父親我的兵》連載至22</a><span><font color=#FF0000>03-05</font></span></li><li><a href='/html/tv/hytv/110863.html' title="2017年香港港臺劇《三個女人一個因(粵語)》連載至5">2017年香港港臺劇《三個女人一個因(粵語)》連載至5</a><span><font color=#FF0000>03-05</font></span></li><li><a href='/html/tv/hytv/110891.html' title="2017年大陸國產劇《人生若如初相見》連載至8">2017年大陸國產劇《人生若如初相見》連載至8</a><span><font color=#FF0000>03-04</font></span></li><li><a href='/html/tv/hytv/110896.html' title="2017年大陸國產劇《利刃出擊》連載至7">2017年大陸國產劇《利刃出擊》連載至7</a><span><font color=#FF0000>03-04</font></span></li><li><a href='/html/tv/hytv/110897.html' title="2017年大陸國產劇《美好生活》連載至8">2017年大陸國產劇《美好生活》連載至8</a><span><font color=#FF0000>03-04</font></span></li><li><a href='/html/tv/hytv/110895.html' title="2017年大陸國產劇《烈火如歌》連載至6">2017年大陸國產劇《烈火如歌》連載至6</a><span><font color=#FF0000>03-04</font></span></li><li><a href='/html/tv/hytv/110790.html' title="2017年大陸國產劇《談判官》連載至43">2017年大陸國產劇《談判官》連載至43</a><span><font color=#FF0000>03-04</font></span></li><li><a href='/html/tv/hytv/109048.html' title="2017年香港港臺劇《愛回家之開心速遞》連載至268">2017年香港港臺劇《愛回家之開心速遞》連載至268</a><span><font color=#FF0000>03-04</font></span></li><li><a href='/html/tv/hytv/110820.html' title="2017年大陸國產劇《櫃中美人》連載至29">2017年大陸國產劇《櫃中美人》連載至29</a><span><font color=#FF0000>03-04</font></span></li><li><a href='/html/tv/hytv/110763.html' title="2017年大陸國產劇《東山晴後雪》連載至28">2017年大陸國產劇《東山晴後雪》連載至28</a><span><font color=#FF0000>03-02</font></span></li><li><a href='/html/tv/hytv/110696.html' title="2017年大陸國產劇《警犬來啦》連載至47">2017年大陸國產劇《警犬來啦》連載至47</a><span><font color=#FF0000>03-01</font></span></li></ul>'''
pattern = re.compile('''<a href='(.*?)' title="(.*?)"''')
# 返回的數據是一個類表嵌套元祖,元祖中放的就是分組數據
res = re.findall(pattern, string4)
for r in res:
    # 正則中包含()分組,group獲取到的匹配的字符串
    print('鏈接:{} 名稱:{}'.format(r[0], r[1]))

# 返回的是一個對象,findall結果稍有不同
res = re.search(pattern, string4)
if res:
    # 正則中包含()分組,group獲取到的匹配的字符串
    print(res.group())
    # 通過分組的索引,獲取對應小分組的信息,分組的索引從1開始
    print(res.group(1))
    print(res.group(2))
else:
    print('沒有匹配到數據')

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