Python中正則表達(dá)式的用法總結(jié)
更新時(shí)間:2019年02月22日 11:59:30 作者:topleeyap
今天小編就為大家分享一篇關(guān)于Python中正則表達(dá)式的用法總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
正則表達(dá)式很神奇啊
# -*- coding:utf-8 -*-
import re
def print_match_res(res):
"""打印匹配對(duì)象內(nèi)容"""
if res is not None:
print(res.group())
else:
print(None)
# 兩種匹配方式:
pattern="[A-Z][a-z]+"
# 一、使用re模塊函數(shù)進(jìn)行匹配
res=re.match(pattern,"Tom is a good boy") # 匹配,返回匹配對(duì)象
print(type(res))
print(res.group())
# 二、使用預(yù)編譯后的正則表達(dá)式對(duì)象的方法進(jìn)行匹配
obj_pattern=re.compile(pattern) # 預(yù)編譯,返回正則表達(dá)式對(duì)象
print(type(obj_pattern))
res=obj_pattern.match("Tom is a good boy") # 匹配,返回匹配對(duì)象
print(type(res))
print(res.group())
# 匹配對(duì)象的group()和groups()方法
pattern="\d{3}-\d{5}"
obj_pattern=re.compile(pattern)
res=obj_pattern.search("家庭電話:000-88886")
print(res.group()) # 返回整個(gè)匹配或特定子組
print(res.groups()) # 返回包含全部子組的元組
# match():從起始部分開始匹配,如果成功,返回匹配對(duì)象;失敗,返回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(): 從任意位置開始匹配,如果成功,返回匹配對(duì)象;失敗,返回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)
# 擇一匹配符號(hào) 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)
# 匹配任意單個(gè)字符 .
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)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
淺析pytest?鉤子函數(shù)?之初始鉤子和引導(dǎo)鉤子
這篇文章主要介紹了pytest?鉤子函數(shù)?之初始鉤子和引導(dǎo)鉤子,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
在python3中使用Supervisor的詳細(xì)教程
Supervisor是用Python開發(fā)的一個(gè)client/server服務(wù),是Linux/Unix系統(tǒng)下的一個(gè)進(jìn)程管理工具,不支持Windows系統(tǒng),本文給大家介紹在python3中使用Supervisor的方法,感興趣的朋友一起看看吧2022-01-01
重寫django的model下的objects模型管理器方式
這篇文章主要介紹了重寫django的model下的objects模型管理器方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05
使用sklearn的cross_val_score進(jìn)行交叉驗(yàn)證實(shí)例
今天小編就為大家分享一篇使用sklearn的cross_val_score進(jìn)行交叉驗(yàn)證實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
python入門課程第一講之安裝與優(yōu)缺點(diǎn)介紹
這篇文章主要介紹了python入門課程第一講之安裝與優(yōu)缺點(diǎn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09

