re模塊介紹:
Python的re模塊提供了正則表達(dá)式的功能,可以用來(lái)進(jìn)行高級(jí)的字符串匹配和處理。re模塊的主要功能包括:
-
編譯正則表達(dá)式 - 使用re.compile()可以編譯正則表達(dá)式字符串,生成正則表達(dá)式對(duì)象。
-
匹配字符串 - 使用正則表達(dá)式對(duì)象的match()、search()、findall()等方法可以在字符串進(jìn)行匹配。
-
替換字符串 - 使用sub()和subn()方法可以使用正則表達(dá)式進(jìn)行字符串替換。
-
分割字符串 - 使用split()方法可以按照正則表達(dá)式的匹配結(jié)果分割字符串。
-
獲取匹配信息 - match對(duì)象包含了各種匹配信息,如匹配字符串、位置等。
-
標(biāo)志 - 可以使用標(biāo)志來(lái)修改正則表達(dá)式的匹配方式,如忽略大小寫(xiě),多行匹配等。
-
模塊級(jí)函數(shù) - re模塊還提供了模塊級(jí)的正則匹配函數(shù),如escape()可以對(duì)字符串轉(zhuǎn)義。
re模塊的這些功能覆蓋了正則表達(dá)式的常見(jiàn)用法。使用re模塊可以簡(jiǎn)化字符串的模式匹配、信息提取、過(guò)濾替換、切分等操作
需要注意的一點(diǎn)是,re模塊主要針對(duì)ASCII字符,對(duì)Unicode的支持不太友好。此時(shí)可以考慮第三方模塊如regex
總之,re模塊是Python中使用正則表達(dá)式的最基礎(chǔ)的模塊,非常值得學(xué)習(xí)和掌握
Python re模塊詳解
re模塊提供正則表達(dá)式模式匹配操作,主要有以下函數(shù):
match()
匹配字符串開(kāi)頭位置,返回match對(duì)象或None:
import re
m = re.match('foo','foo')
print(m.group()) # 'foo'
m = re.match('foo','bar')
print(m) # None
search()
搜索字符串任意位置,返回match對(duì)象或None:
m = re.search('foo','hello food')
print(m.group()) # 'foo'
findall()
搜索字符串,返回所有匹配的列表:
m = re.findall('\d','123abc456')
print(m) # ['1', '2', '3', '4', '5', '6']
sub()
使用正則表達(dá)式進(jìn)行字符串替換:
text = re.sub('\d', '0', '123abc456')
print(text) # '000abc000'
split()
使用正則表達(dá)式進(jìn)行字符串分割:
m = re.split('\d+', '123abc456')
print(m) # ['abc', '']
compile()
編譯正則表達(dá)式,返回pattern對(duì)象:
pat = re.compile('\d')
m = pat.match('123')
finditer()
在Python的re模塊中,re.finditer()
是非常有用的一個(gè)正則表達(dá)式匹配函數(shù)。
re.finditer()
的作用是在字符串中找到所有的匹配,并返回一個(gè)迭代器。相比re.findall()
和re.finditer()
有以下區(qū)別:
-
re.findall()
:返回一個(gè)匹配字符串的列表 -
re.finditer()
:返回一個(gè)匹配對(duì)象迭代器
示例:
import re
s = 'hello 123 456 world'
matches = re.findall('\d+', s)
print(matches) # ['123', '456']
iterator = re.finditer('\d+', s)
print(iterator) # <callable_iterator object at 0x10f5f3b50>
for match in iterator:
print(match)
# <re.Match object; span=(6, 9), match='123'>
# <re.Match object; span=(10, 13), match='456'>
re.finditer()
的返回對(duì)象是一個(gè)迭代器,每次迭代返回一個(gè)Match對(duì)象,包含匹配的字符串和位置。
主要優(yōu)點(diǎn)是:
- 不需要先存儲(chǔ)所有匹配,更save內(nèi)存
- 可以逐個(gè)訪問(wèn)每個(gè)匹配
- 提供了匹配的位置信息
所以在需要定位每個(gè)匹配的位置時(shí),re.finditer()
非常有用。
fullmatch()
匹配整個(gè)字符串,返回match對(duì)象或None:
import re
m = re.fullmatch('foo','foo')
print(m.group()) # 'foo'
m = re.fullmatch('foo','foo bar')
print(m) # None
escape()
將特殊字符轉(zhuǎn)義,可以將字符串轉(zhuǎn)化為正則表達(dá)式的字符串形式:
escaped = re.escape('http://example.com')
print(escaped) # 'http:\/\/example\.com'
purge()
清除緩存的正則表達(dá)式,可以避免重復(fù)編譯正則表達(dá)式:
pat = re.compile(r'\d+')
re.purge() # 清除緩存
match.expand()
使用匹配到的組內(nèi)容,替換字符串模板:
m = re.match(r'(?P<name>\w+) (\w+)', 'John Doe')
print(m.expand('Hello \g<name>')) # 'Hello John'
(?P\w+)和 group(“name”) 搭配使用
import re
pattern = r'(?P<first_name>\w+) (?P<last_name>\w+)'
string = 'John Doe'
# 匹配字符串
m = re.match(pattern, string)
# 使用命名組獲取匹配
first_name = m.group('first_name')
last_name = m.group('last_name')
print(first_name) # John
print(last_name) # Doe
# 替換字符串
new_string = re.sub(pattern, r'\g<last_name>, \g<first_name>', string)
print(new_string) # Doe, John
在這個(gè)例子中,正則表達(dá)式模式使用了兩個(gè)命名捕獲組first_name和last_name。
然后在獲取匹配后,可以直接通過(guò)命名引用匹配的內(nèi)容。
在替換字符串時(shí),也可以利用命名組引用,使代碼更簡(jiǎn)潔清晰。
所以命名捕獲組可以讓正則匹配和處理更高效方便。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-629974.html
以上是re模塊的常用函數(shù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-629974.html
到了這里,關(guān)于Python-re模塊-正則表達(dá)式模塊常用方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!