python的re模塊使用方法詳解
一、正則表達式的特殊字符介紹
正則表達式
^ 匹配行首
$ 匹配行尾
. 任意單個字符
[] 匹配包含在中括號中的任意字符
[^] 匹配包含在中括號中的字符之外的字符
[-] 匹配指定范圍的任意單個字符
? 匹配之前項的1次或者0次
+ 匹配之前項的1次或者多次
* 匹配之前項的0次或者多次
{n} 匹配之前項的n次
{m,n} 匹配之前項最大n次,最小m次
{n,} 配置之前項至少n次
二、re模塊的方法介紹
1、匹配類方法
a、findall方法
# findall方法,該方法在字符串中查找模式匹配,將所有的匹配字符串以列表的形式返回,如果文本中沒有任何字符串匹配模式,則返回一個空的列表, # 如果有一個子字符串匹配模式,則返回包含一個元素的列表,所以,無論怎么匹配,我們都可以直接遍歷findall返回的結果而不會出錯,這對工程師 # 編寫程序來說,減少了異常情況的處理,代碼邏輯更加簡潔
# re.findall() 用來輸出所有符合模式匹配的子串
re_str = "hello this is python 2.7.13 and python 3.4.5"
pattern = "python [0-9]\.[0-9]\.[0-9]"
res = re.findall(pattern=pattern,string=re_str)
print(res)
# ['python 2.7.1', 'python 3.4.5']
pattern = "python [0-9]\.[0-9]\.[0-9]{2,}"
res = re.findall(pattern=pattern,string=re_str)
print(res)
# ['python 2.7.13']
pattern = "python[0-9]\.[0-9]\.[0-9]{2,}"
res = re.findall(pattern=pattern,string=re_str)
print(res)
# []
# re.findall() 方法,返回一個列表,如果匹配到的話,列表中的元素為匹配到的子字符串,如果沒有匹配到,則返回一個空的列表
re_str = "hello this is python 2.7.13 and Python 3.4.5"
pattern = "python [0-9]\.[0-9]\.[0-9]"
res = re.findall(pattern=pattern,string=re_str,flags=re.IGNORECASE)
print(res)
# ['python 2.7.1', 'Python 3.4.5']
# 設置標志flags=re.IGNORECASE,意思為忽略大小寫
b、編譯的方式使用正則表達式
# 我們一般采用編譯的方式使用python的正則模塊,如果在大量的數(shù)據(jù)量中,編譯的方式使用正則性能會提高很多,具體讀者們可以可以實際測試 re_str = "hello this is python 2.7.13 and Python 3.4.5" re_obj = re.compile(pattern = "python [0-9]\.[0-9]\.[0-9]",flags=re.IGNORECASE) res = re_obj.findall(re_str) print(res)
c、match方法
# match方法,類似于字符串中的startwith方法,只是match應用在正則表達式中更加強大,更富有表現(xiàn)力,match函數(shù)用以匹配字符串的開始部分,如果模式 # 匹配成功,返回一個SRE_Match類型的對象,如果模式匹配失敗,則返回一個None,因此對于普通的前綴匹配,他的用法幾乎和startwith一模一樣,例如我 # 們要判斷data字符串是否以what和是否以數(shù)字開頭
s_true = "what is a boy"
s_false = "What is a boy"
re_obj = re.compile("what")
print(re_obj.match(string=s_true))
# <_sre.SRE_Match object; span=(0, 4), match='what'
print(re_obj.match(string=s_false))
# None
s_true = "123what is a boy"
s_false = "what is a boy"
re_obj = re.compile("\d+")
print(re_obj.match(s_true))
# <_sre.SRE_Match object; span=(0, 3), match='123'>
print(re_obj.match(s_true).start())
# 0
print(re_obj.match(s_true).end())
# 3
print(re_obj.match(s_true).string)
# 123what is a boy
print(re_obj.match(s_true).group())
# 123
print(re_obj.match(s_false))
# None
d、search方法
# search方法,模式匹配成功后,也會返回一個SRE_Match對象,search方法和match的方法區(qū)別在于match只能從頭開始匹配,而search可以從 # 字符串的任意位置開始匹配,他們的共同點是,如果匹配成功,返回一個SRE_Match對象,如果匹配失敗,返回一個None,這里還要注意, # search僅僅查找第一次匹配,也就是說一個字符串中包含多個模式的匹配,也只會返回第一個匹配的結果,如果要返回所有的結果,最簡單 # 的方法就是findall方法,也可以使用finditer方法
e、finditer方法
# finditer返回一個迭代器,遍歷迭代器可以得到一個SRE_Match對象,比如下面的例子
re_str = "what is a different between python 2.7.14 and python 3.5.4"
re_obj = re.compile("\d{1,}\.\d{1,}\.\d{1,}")
for i in re_obj.finditer(re_str):
print(i)
# <_sre.SRE_Match object; span=(35, 41), match='2.7.14'>
# <_sre.SRE_Match object; span=(53, 58), match='3.5.4'>
2、修改類方法介紹
a、sub方法
# re模塊sub方法類似于字符串中的replace方法,只是sub方法支持使用正則表達式,所以,re模塊的sub方法使用場景更加廣泛
re_str = "what is a different between python 2.7.14 and python 3.5.4"
re_obj = re.compile("\d{1,}\.\d{1,}\.\d{1,}")
print(re_obj.sub("a.b.c",re_str,count=1))
# what is a different between python a.b.c and python 3.5.4
print(re_obj.sub("a.b.c",re_str,count=2))
# what is a different between python a.b.c and python a.b.c
print(re_obj.sub("a.b.c",re_str))
# what is a different between python a.b.c and python a.b.c
b、split方法
# re模塊的split方法和python字符串中的split方法功能是一樣的,都是將一個字符串拆分成子字符串的列表,區(qū)別在于re模塊的split方法能夠 # 使用正則表達式 # 比如下面的例子,使用. 空格 : !分割字符串,返回的是一個列表
re_str = "what is a different between python 2.7.14 and python 3.5.4 USA:NewYork!Zidan.FRA"
re_obj = re.compile("[. :!]")
print(re_obj.split(re_str))
# ['what', 'is', 'a', 'different', 'between', 'python', '2', '7', '14', 'and', 'python', '3', '5', '4', 'USA', 'NewYork', 'Zidan', 'FRA']
c、大小寫不敏感設置
# 3、大小寫不敏感 # re.compile(flags=re.IGNORECASE)
d、非貪婪匹配
# 4、非貪婪匹配,貪婪匹配總是匹配到最長的那個字符串,相應的,非貪婪匹配是匹配到最小的那個字符串,只需要在匹配字符串的時候加一個?即可
# 下面的例子,注意兩個.
s = "Beautiful is better than ugly.Explicit is better than impliciy."
re_obj = re.compile("Beautiful.*y\.")
print(re_obj.findall(s))
# ['Beautiful is better than ugly.Explicit is better than implicit.']
re_obj = re.compile("Beautiful.*?\.")
print(re_obj.findall(s))
# ['Beautiful is better than ugly.']
e、在正則匹配字符串中加一個小括號,會有什么的效果呢?
如果是要配置一個真正的小括號,那么就需要轉義符,下面的例子大家仔細看下,注意下search方法返回的對象的group(1)這個方法是報錯的
import re
s = "=aa1239d&&& 0a ()--"
# obj = re.compile("\(\)")
# search
# rep = obj.search(s)
# print(rep)
# <_sre.SRE_Match object; span=(15, 17), match='()'>
# print(rep.group(1))
# IndexError: no such group
# print(rep.group())
# ()
# findall rep = obj.findall(s) print(rep) # ['()']
如果是要返回括號中匹配的字符串中,則該小括號不需要轉義符,findall方法返回的是小伙好中匹配到的字符串,search.group()方法的返回的整個模式匹配到字符串,search.group(1)這個是匹配第一個小括號中的模式匹配到的字符串,search.group(2)這個是匹配第二個小括號中的模式匹配到的字符串,以此類推
s = "=aa1239d&&& 0a ()--"
rep = re.compile("\w+(&+)")
print(rep.findall(s))
# ['&&&']
print(rep.search(s).group())
# aa1239d&&&
print(rep.search(s).group(1))
# &&&
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python-apply(lambda x: )的使用及說明
這篇文章主要介紹了Python-apply(lambda x: )的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
python selenium 對瀏覽器標簽頁進行關閉和切換的方法
今天小編就為大家分享一篇python selenium 對瀏覽器標簽頁進行關閉和切換的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
詳解如何使用Plotly和Dash進行數(shù)據(jù)可視化
數(shù)據(jù)可視化是數(shù)據(jù)分析中至關重要的一環(huán),它能夠幫助我們更直觀地理解數(shù)據(jù)并發(fā)現(xiàn)隱藏的模式和趨勢,本文將介紹如何使用Plotly和Dash進行數(shù)據(jù)可視化,感興趣的可以了解下2024-04-04
從零學python系列之新版本導入httplib模塊報ImportError解決方案
在使用新版python打開舊版本代碼的時候,可能會有些報錯或者不兼容的情況出現(xiàn),今天我們就來分析其中的一種情況2014-05-05

