日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Python 正则 —— 捕获与分组

發(fā)布時(shí)間:2024/9/5 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 正则 —— 捕获与分组 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
  • \n:表示第 n 個捕獲:

    >> s = "<html><h1>what the fuck!</h1></html>" >> p = r"<(.+)><(.+)>(.+)</\2></\1>"# \2 對應(yīng)第二個捕獲,也即 h1,則 </\2> 為:</h1># \1 對應(yīng)第一個捕獲,也即 html,則 </\1> 為:</html> >> re.match(p, s).group(3) 'what the fuck!'

1. 匹配郵箱與html標(biāo)簽

  • 匹配郵箱:

    >> mail = 'zch921005@126.com' >> reg = r"(\w{4,20})@(126|qq|gmail|163|outlook)\.(com)"# 正則表達(dá)式中不要出現(xiàn)無意義的空格 >> re.match(reg, mail).group(1) 'zch921005' >> re.match(reg, mail).group(2) '126' >>
  • 匹配 html 標(biāo)簽:

    >> s='<div><a href="https://support.google.com/chrome/?p=ui_hotword_search" rel="external nofollow" target="_blank">更多</a><p>dfsl</p></div>' >> re.search(r'<a.*>(.*)</a>', s).group(1) '更多'

2. 起別名

https://blog.csdn.net/HeatDeath/article/details/70171569

>>> s = '<html><h1>what the fuck!</h1></html>' >>> p = r"<(?P<key1>.+)><(?P<key2>.+)>(.+)</(?P=key2)></(?P=key1)>" >> re.match(p, s).group(1) 'html' >> re.match(p, s).group(2) 'h1' >> re.match(p, s).group(3) 'what the fuck!'

轉(zhuǎn)載于:https://www.cnblogs.com/mtcnn/p/9420949.html

總結(jié)

以上是生活随笔為你收集整理的Python 正则 —— 捕获与分组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。