生活随笔
收集整理的這篇文章主要介紹了
python——正则表达式详解(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文講解正則表達式常用的幾大函數方法:
方法使用格式講解
| match | match(string, pos=0, endpos=-1) | 方法用于查找字符串的頭部,它只要找到了一個匹配的結果就返回 |
| search | search(string, pos=0, endpos=-1) | 方法用于查找字符串的任何位置,它只要找到一個匹配的結果就返回 |
| findall | findall(string, pos=0, endpos=-1) | 方法返回所有匹配的結果 |
| finditer | finditer(string, pos=0, endpos=-1) | 通過該迭代器我們可以訪問匹配的每一個字符串 |
| split | slit(strng, maxsplit=0) | 方法表示將能夠匹配的子串切割 |
| sub | sub(repl, string, count=0) | 方法用來替換 |
| subn | subn(repl, string, count=0) | 方法也是用于替換,返回一個元組 |
1.match方法講解
該方法用于查找字符串的頭部,它只要找到了一個匹配的結果就返回,說明:string是待匹配的字符串,pos和endpos指定字符串的起點和終點的位置,當不指定時,默認從頭部開始匹配,當匹配成功時,返回Match對象。
import re
"""
match(string, pos=0, endpos=-1)
該方法用于查找字符串的頭部,它只要找到了一個匹配的結果就返回
說明:string是待匹配的字符串,pos和endpos指定字符串的起點和終點的位置
當不指定時,默認從頭部開始匹配,當匹配成功時,返回Match對象
"""
test_str
= "aaaa12345678sfsdf"
pattern
= re
.compile(r"\d+")
matcher
= pattern
.match
(test_str
, 4, 12)
print(matcher
)
print(matcher
.group
())
print(matcher
.start
())
print(matcher
.end
())
print(matcher
.span
())
2.search方法講解
該方法用于查找字符串的任何位置,它只要找到一個匹配的結果就返回,
說明:string是帶匹配的字符串,pos和endpos分別為字符串的起始和結束位置,
當匹配成功時返回Match對象,匹配不成功時返回None。
import re
"""
search(string, pos=0, endpos=-1)
該方法用于查找字符串的任何位置,它只要找到一個匹配的結果就返回
說明:string是帶匹配的字符串,pos和endpos分別為字符串的起始和結束位置
當匹配成功時返回Match對象,匹配不成功時返回None
"""
test_str
= "aaa777bbb888ccc999"
pattern
= re
.compile(r"\d+")
matcher
= pattern
.search
(test_str
)
print(matcher
)
test_str
= "aaa000bbb111ccc222"
pattern
= re
.compile(r"\d+")
matcher
= pattern
.search
(test_str
, 3, 12)
print(matcher
)
print(matcher
.group
())
print(matcher
.start
())
print(matcher
.end
())
print(matcher
.span
())
3.findall方法講解
該方法返回所有匹配的結果,
說明:string是帶匹配的字符串,pos和endpos分別為字符串的起始和結束位置,
當匹配成功時返回匹配的列表,匹配不成功時返回空列表,
捕獲:findall函數,在正則匹配里,如果有分組,就僅僅匹配分組里面的內容,
然后返回這個組的列表,如果有多個分組,那就把每一個分組看成一個單位,
組合成一個元組,然后返回一個含有多個元組的列表。
import re
"""
findall(string, pos=0, endpos=-1)
該方法返回所有匹配的結果
說明:string是帶匹配的字符串,pos和endpos分別為字符串的起始和結束位置,
當匹配成功時返回匹配的列表,匹配不成功時返回空列表,
捕獲:findall函數,在正則匹配里,如果有分組,就僅僅匹配分組里面的內容,
然后返回這個組的列表,如果有多個分組,那就把每一個分組看成一個單位,
組合成一個元組,然后返回一個含有多個元組的列表。
"""
test_str
= "aaa111bbb222ccc333"
pattern
= re
.compile(r"\d+")
matcher
= pattern
.findall
(test_str
)
print(matcher
)
test_str
= "aaa111bbb222ccc333"
pattern
= re
.compile(r"\d+")
matcher
= pattern
.findall
(test_str
, 2, 13)
print(matcher
)
4.finditer方法講解
說明:string是帶匹配的字符串,pos和endpos分別為字符串的起始和結束位置,
說明:該方法返回所有匹配的字符串,但是它返回的是一個迭代器,
通過該迭代器我們可以訪問匹配的每一個字符串。
import re
"""
finditer(string, pos=0, endpos=-1)
說明:string是帶匹配的字符串,pos和endpos分別為字符串的起始和結束位置,
說明:該方法返回所有匹配的字符串,但是它返回的是一個迭代器
通過該迭代器我們可以訪問匹配的每一個字符串
"""
test_str
= "aaa111bbb222ccc333"
pattern
= re
.compile(r"\d+")
matcher
= pattern
.finditer
(test_str
)
print(matcher
)
for result
in matcher
:print("找到的字符串{0},位置是{1}".format(result
.group
(), result
.span
()))
test_str
= "aaa111bbb222ccc333"
pattern
= re
.compile(r"\d+")
matcher
= pattern
.finditer
(test_str
, 3, 12)
print(matcher
)
for result
in matcher
:print("找到的字符串{0},位置是{1}".format(result
.group
(), result
.span
()))
5.split方法講解
該方法表示將能夠匹配的子串切割,說明:string表示需要匹配的字符串,
maxsplit表示最大的分割次數,不指定即為全部分割。
import re
"""
slit(strng, maxsplit=0)
說明:該方法表示將能夠匹配的子串切割,string表示需要匹配的字符串,
maxsplit表示最大的分割次數,不指定即為全部分割
"""
test_str
= "aaaa bbbb cccc,dddd;eeee"
pattern
= re
.compile(r"[,;\s]+")
matcher
= pattern
.split
(test_str
)
print(matcher
)
test_str
= "aaaa bbbb cccc,dddd;eeee"
pattern
= re
.compile(r"[,;\s]+")
matcher
= pattern
.split
(test_str
, 2)
print(matcher
)
6.sub方法講解
該方法用來替換,
說明:repl如果為字符串,會使用repl替換字符串中的每一個匹配的子串,并且返回替換后的字符串,
如果為函數,則該函數應該只接收一個Match對象,并且返回一個字符串用于替換,
count用于指定替換次數。
import re
"""
sub(repl, string, count=0)
該方法用來替換
說明:repl如果為字符串,會使用repl替換字符串中的每一個匹配的子串,并且返回替換后的字符串,
如果為函數,則該函數應該只接收一個Match對象,并且返回一個字符串用于替換,
count用于指定替換次數。
"""
test_str
= "aaaa bbbb cccc,dddd;eeee"
pattern
= re
.compile(r"[,;\s]+")
repl_str
= "&"
matcher
= pattern
.sub
( repl_str
, test_str
)
print(matcher
)
test_str
= "aaaa bbbb cccc,dddd;eeee"
pattern
= re
.compile(r"[,;\s]+")def func(m
):print("m:", m
)return "*"matcher
= pattern
.sub
(func
, test_str
, 3)
print(matcher
)
7.subn方法講解
該方法也是用于替換,說明:返回一個元組,元組有兩個元素,
第一個和使用sub方法返回的結果一致,另一個表示替換的次數。
import re
"""
subn(repl, string, count=0)
說明:該方法也是用于替換,返回一個元組,元組有兩個元素,
第一個和使用sub方法返回的結果一致,另一個表示替換的次數。
"""
test_str
= "aaaa bbbb cccc,dddd;eeee"
pattern
= re
.compile(r"[,;\s]+")
repl_str
= "&"
matcher
= pattern
.subn
(repl_str
, test_str
)
print(matcher
)
test_str
= "aaaa bbbb cccc,dddd;eeee"
pattern
= re
.compile(r"[,;\s]+")def func(m
):print("m:", m
)return "*"matcher
= pattern
.subn
(func
, test_str
, 3)
print(matcher
)
以上是python關于正則表達式的講解之二,后續會繼續更新正則表達式的經典使用案例,有疑問的歡迎評論或私信博主啊。
總結
以上是生活随笔為你收集整理的python——正则表达式详解(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。