最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中正則表達式的用法總結(jié)

 更新時間:2019年02月22日 11:59:30   作者:topleeyap  
今天小編就為大家分享一篇關(guān)于Python中正則表達式的用法總結(jié),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

正則表達式很神奇啊

# -*- coding:utf-8 -*-
import re
def print_match_res(res):
  """打印匹配對象內(nèi)容"""
  if res is not None:
    print(res.group())
  else:
    print(None)
# 兩種匹配方式:
pattern="[A-Z][a-z]+"
# 一、使用re模塊函數(shù)進行匹配
res=re.match(pattern,"Tom is a good boy")     # 匹配,返回匹配對象
print(type(res))
print(res.group())
# 二、使用預(yù)編譯后的正則表達式對象的方法進行匹配
obj_pattern=re.compile(pattern)   # 預(yù)編譯,返回正則表達式對象
print(type(obj_pattern))
res=obj_pattern.match("Tom is a good boy")    # 匹配,返回匹配對象
print(type(res))
print(res.group())
# 匹配對象的group()和groups()方法
pattern="\d{3}-\d{5}"
obj_pattern=re.compile(pattern)
res=obj_pattern.search("家庭電話:000-88886")
print(res.group())   # 返回整個匹配或特定子組
print(res.groups())   # 返回包含全部子組的元組
# match():從起始部分開始匹配,如果成功,返回匹配對象;失敗,返回None。只匹配一次
pattern="my"
# res=re.compile(pattern).match("my name is li")
res=re.match(pattern,"my name is li")
print_match_res(res)
# search(): 從任意位置開始匹配,如果成功,返回匹配對象;失敗,返回None。只匹配一次
pattern="my"
# res=re.compile(pattern).search("it's my dog")
res=re.search(pattern,"my name is li")
print_match_res(res)
# 查找全部
# findall(),finditer()
res=re.findall(r"th\w+","This and that",re.I)
print(res)
res=re.finditer(r"th\w+","This and that",re.I)
print(res)
print(next(res).group(),next(res).group())
# 替換
# sub(),subn()
res=re.sub("funny","fool","You are so funny")
print(res)
res=re.subn("funny","fool","You are so funny")
print(res)
# 分割
# splite()
res=re.split("\.","Mr.Smith")
print(res)
print("#"*50)
# 擇一匹配符號 a|b
pattern="I|You|She"
res=re.compile(pattern,flags=re.IGNORECASE).match("i love you")
print_match_res(res)
res=re.compile(pattern,flags=re.I).search("who love you")
print_match_res(res)
# 匹配任意單個字符 .
pattern="w{3,}\..+\.com"
res=re.match(pattern,"wwww.google.com/index.html",re.I)
print_match_res(res)
# 字符集 [abc] [a-z0-9]
pattern="[A-Za-z0-9_]*\."
res=re.match(pattern,"Python3.?")
print_match_res(res)
# 特殊字符 \d \w \s \b \\
# 重復(fù) + ? * {N,} {N,M}
# 分組 (...)
pattern="\w+@(\w{1,10}\.)*([a-z]*)"
res=re.match(pattern,"li@gmail.com")
print_match_res(res)
res=re.match(pattern,"li@qq.vip.org")
print_match_res(res)
print(res.group(0),res.group(1),res.group(2),sep="\t")
print(res.groups())
# 匹配字符串的起始和結(jié)尾,單詞邊界 ^a z$ \A \Z \b \B
pattern=r"^the"
# pattern=r"\Athe"
res=re.search(pattern,"The end of the world")
print_match_res(res)
res=re.search(pattern,"they smile")
print_match_res(res)
pattern=r"cry$"
# pattern=r"cry\Z"
res=re.search(pattern,"they cry")
print_match_res(res)
res=re.search(r"\bthe","bit the dog")
print_match_res(res)
res=re.search(r"\Bhe","bit the dog")
print_match_res(res)

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • Python文件操作JSON CSV TSV Excel和Pickle文件序列化

    Python文件操作JSON CSV TSV Excel和Pickle文件序列化

    這篇文章主要為大家介紹了Python文件操作之JSON、CSV、TSV、Excel和Pickle文件序列化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • 利用Python?實現(xiàn)分布式計算

    利用Python?實現(xiàn)分布式計算

    這篇文章主要介紹了利用Python?實現(xiàn)分布式計算,文章通過借助于?Ray展開對分布式計算的實現(xiàn),感興趣的小伙伴可以參考一下
    2022-05-05
  • python?PyVCF文件處理VCF文件格式實例詳解

    python?PyVCF文件處理VCF文件格式實例詳解

    這篇文章主要為大家介紹了python?PyVCF文件處理VCF文件格式實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • python全棧開發(fā)語法總結(jié)

    python全棧開發(fā)語法總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于python全棧開發(fā)語法的相關(guān)總結(jié)內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)參考下。
    2020-11-11
  • Python SSL證書驗證問題解決方案

    Python SSL證書驗證問題解決方案

    這篇文章主要介紹了Python SSL證書驗證問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01
  • Python進階教程之創(chuàng)建本地PyPI倉庫

    Python進階教程之創(chuàng)建本地PyPI倉庫

    pypi是一個python包的倉庫,里面有很多別人寫好的python庫,你可以通過easy_install或者pip進行安裝,下面這篇文章主要給大家介紹了關(guān)于Python進階教程之創(chuàng)建本地PyPI倉庫的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • 淺談python3.x pool.map()方法的實質(zhì)

    淺談python3.x pool.map()方法的實質(zhì)

    這篇文章主要介紹了python3.x pool.map方法的實質(zhì),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • python實現(xiàn)圖片批量剪切示例

    python實現(xiàn)圖片批量剪切示例

    這篇文章主要介紹了python實現(xiàn)圖片批量剪切示例,需要的朋友可以參考下
    2014-03-03
  • Python常見數(shù)據(jù)結(jié)構(gòu)詳解

    Python常見數(shù)據(jù)結(jié)構(gòu)詳解

    這篇文章主要介紹了Python常見數(shù)據(jù)結(jié)構(gòu),需要的朋友可以參考下
    2014-07-07
  • 終端能到import模塊 解決jupyter notebook無法導(dǎo)入的問題

    終端能到import模塊 解決jupyter notebook無法導(dǎo)入的問題

    這篇文章主要介紹了在終端能到import模塊 而在jupyter notebook無法導(dǎo)入的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論

浑源县| 瑞昌市| 浦江县| 阿拉尔市| 明光市| 东兰县| 昭平县| 庆云县| 达日县| 阳曲县| 闽侯县| 宁安市| 永德县| 丽水市| 牡丹江市| 海原县| 大洼县| 客服| 柘城县| 惠东县| 馆陶县| 安溪县| 道孚县| 木兰县| 张家港市| 佛坪县| 安丘市| 扎赉特旗| 鹤壁市| 新野县| 舞阳县| 三台县| 夏河县| 十堰市| 寻甸| 四平市| 休宁县| 伊宁县| 沅江市| 繁峙县| 大新县|