日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python 正则 —— 捕获与分组

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

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

1. 匹配郵箱與html標簽

  • 匹配郵箱:

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

    >> 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!'

轉載于:https://www.cnblogs.com/mtcnn/p/9420949.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。