<h1><strong>一 . 正則表達(dá)式中常用的字符含義<strong><h1>
<h1><strong>二 . re模塊中常用的功能函數(shù)<strong><h1>
<h2>一 . 正則表達(dá)式中常用的字符含義<h2>
下面是Python中正則表達(dá)式的一些匹配規(guī)則诊县,圖片資料來自CSDN
這里需要強調(diào)一下反斜杠\的作用:
1 反斜杠后邊跟元字符去除特殊功能讲弄;(即將特殊字符轉(zhuǎn)義成普通字符)
2 反斜杠后邊跟普通字符實現(xiàn)特殊功能;(即預(yù)定義字符)
3 引用序號對應(yīng)的字組所匹配的字符串依痊。
<h1>>二 . re模塊中常用的功能函數(shù)<h1>
(1)re.match(pattern, string[, flags])
這個方法將會從string(我們要匹配的字符串)的開頭開始避除,嘗試匹配pattern,一直向后匹配,如果遇到無法匹配的字符驹饺,立即返回None钳枕,如果匹配未結(jié)束已經(jīng)到達(dá)string的末尾,也會返回None赏壹。兩個結(jié)果均表示匹配失敗鱼炒,否則匹配pattern成功,同時匹配終止蝌借,不再對string向后匹配昔瞧。下面我們通過一個例子理解一下
# -*- coding: utf-8 -*-
#導(dǎo)入re模塊
import re
# 將正則表達(dá)式編譯成Pattern對象,注意hello前面的r的意思是“原生字符串”
pattern = re.compile(r'hello')
# 使用re.match匹配文本菩佑,獲得匹配結(jié)果自晰,無法匹配時將返回None
result1 = re.match(pattern,'hello')
result2 = re.match(pattern,'helloo CQC!')
result3 = re.match(pattern,'helo CQC!')
result4 = re.match(pattern,'hello CQC!')
#如果1匹配成功
if result1:
# 使用Match獲得分組信息
print result1.group()
else:
print '1匹配失敗稍坯!'
#如果2匹配成功
if result2:
# 使用Match獲得分組信息
print result2.group()
else:
print '2匹配失敵贶瘛!'
#如果3匹配成功
if result3:
# 使用Match獲得分組信息
print result3.group()
else:
print '3匹配失斍朴础混巧!'
#如果4匹配成功
if result4:
# 使用Match獲得分組信息
print result4.group()
else:
print '4匹配失敗勤揩!'
結(jié)果
hello
hello
3匹配失敻古舌界!
hello
匹配分析
1.第一個匹配,pattern正則表達(dá)式為’hello’,我們匹配的目標(biāo)字符串string也為hello羡洁,從頭至尾完全匹配牵舱,匹配成功九巡。
2.第二個匹配帅腌,string為helloo CQC,從string頭開始匹配pattern完全可以匹配遮糖,pattern匹配結(jié)束箱残,同時匹配終止,后面的o CQC不再匹配止吁,返回匹配成功的信息被辑。
3.第三個匹配,string為helo CQC敬惦,從string頭開始匹配pattern盼理,發(fā)現(xiàn)到 ‘o’ 時無法完成匹配,匹配終止俄删,返回None
4.第四個匹配宏怔,同第二個匹配原理奏路,即使遇到了空格符也不會受影響。
我們還看到最后打印出了result.group()臊诊,這個是什么意思呢鸽粉?下面我們說一下關(guān)于match對象的的屬性和方法
Match對象是一次匹配的結(jié)果,包含了很多關(guān)于此次匹配的信息抓艳,可以使用Match提供的可讀屬性或方法來獲取這些信息触机。
(2)re.search(pattern, string[, flags])
search方法與match方法極其類似,區(qū)別在于match()函數(shù)只檢測re是不是在string的開始位置匹配玷或,search()會掃描整個string查找匹配儡首,match()只有在0位置匹配成功的話才有返回,如果不是開始位置匹配成功的話偏友,match()就返回None蔬胯。同樣,search方法的返回對象同樣match()返回對象的方法和屬性位他。我們用一個例子感受一下
import re
# 將正則表達(dá)式編譯成Pattern對象
pattern = re.compile(r'world')
# 使用search()查找匹配的子串氛濒,不存在能匹配的子串時將返回None
# 這個例子中使用match()無法成功匹配
match = re.search(pattern,'hello world!')
if match:
# 使用Match獲得分組信息
print match.group()
### 輸出 ###
# world
(3)re.split(pattern, string[, maxsplit])
按照能夠匹配的子串將string分割后返回列表。maxsplit用于指定最大分割次數(shù)鹅髓,不指定將全部分割舞竿。我們通過下面的例子感受一下。
import re
pattern = re.compile(r'\d+')
print re.split(pattern,'one1two2three3four4')
### 輸出 ###
# ['one', 'two', 'three', 'four', '']
(4)re.findall(pattern, string[, flags])
搜索string迈勋,以列表形式返回全部能匹配的子串炬灭。我們通過這個例子來感受一下
import re
pattern = re.compile(r'\d+')
print re.findall(pattern,'one1two2three3four4')
### 輸出 ###
# ['1', '2', '3', '4']
(5)re.finditer(pattern, string[, flags])
搜索string醋粟,返回一個順序訪問每一個匹配結(jié)果(Match對象)的迭代器靡菇。我們通過下面的例子來感受一下
import re
pattern = re.compile(r'\d+')
for m in re.finditer(pattern,'one1two2three3four4'):
print m.group(),
### 輸出 ###
# 1 2 3 4
(6)re.sub(pattern, repl, string[, count])
使用repl替換string中每一個匹配的子串后返回替換后的字符串。
當(dāng)repl是一個字符串時米愿,可以使用\id或\g厦凤、\g引用分組,但不能使用編號0育苟。
當(dāng)repl是一個方法時较鼓,這個方法應(yīng)當(dāng)只接受一個參數(shù)(Match對象),并返回一個字符串用于替換(返回的字符串中不能再引用分組)违柏。
count用于指定最多替換次數(shù)博烂,不指定時全部替換。
import re
pattern = re.compile(r'(\w+) (\w+)')
s = 'i say, hello world!'
print re.sub(pattern,r'\2 \1', s)
def func(m):
return m.group(1).title() + ' ' + m.group(2).title()
print re.sub(pattern,func, s)
### output ###
# say i, world hello!
# I Say, Hello World!
(7)re.subn(pattern, repl, string[, count])
返回 (sub(repl, string[, count]), 替換次數(shù))漱竖。
import re
pattern = re.compile(r'(\w+) (\w+)')
s = 'i say, hello world!'
print re.subn(pattern,r'\2 \1', s)
def func(m):
return m.group(1).title() + ' ' + m.group(2).title()
print re.subn(pattern,func, s)
### output ###
# ('say i, world hello!', 2)
# ('I Say, Hello World!', 2)