**RE.COMPILE、RE.MATCH、RE.SEARCH
import re
help(re.compile)
輸出結(jié)果為:
Help on function compile in module re:
compile(pattern, flags=0)
Compile a regular expression pattern, returning a pattern object.
通過(guò)help可知:編譯一個(gè)正則表達(dá)式模式,返回一個(gè)模式對(duì)象。
第二個(gè)參數(shù)flags是匹配模式,可以使用按位或’|’表示同時(shí)生效,也可以在正則表達(dá)式字符串中指定。
Pattern對(duì)象是不能直接實(shí)例化的,只能通過(guò)compile方法得到。
**匹配模式有:
1).re.I(re.IGNORECASE): 忽略大小寫
2).re.M(MULTILINE): 多行模式,改變’^’和’$’的行為
3).re.S(DOTALL): 點(diǎn)任意匹配模式,改變’.’的行為
4).re.L(LOCALE): 使預(yù)定字符類 \w \W \b \B \s \S 取決于當(dāng)前區(qū)域設(shè)定
5).re.U(UNICODE): 使預(yù)定字符類 \w \W \b \B \s \S \d \D 取決于unicode定義的字符屬性
6).re.X(VERBOSE): 詳細(xì)模式。這個(gè)模式下正則表達(dá)式可以是多行,忽略空白字符,并可以加入注釋
text="JGod is a handsome boy ,but he is a ider"
print re.findall(r'\wo\w',text) #查找有o的單詞
輸出結(jié)果為:['JGod', 'handsome', 'boy']
利用compile生成一個(gè)規(guī)則模式吧,然后利用findall將某一個(gè)對(duì)象內(nèi)容進(jìn)行匹配,合適則輸出符合規(guī)則的內(nèi)容
text="JGod is a handsome boy ,but he is a ider"
regex=re.compile(r'\wo\w')
print regex.findall(text)
>>> ['JGod', 'handsome', 'boy']
test1="who you are,what you do,When you get get there? What is time you state there?"
regex1=re.compile(r'\wwh\w',re.IGNORECASE)#忽略大小寫
wh=regex1.findall(test1)
print wh
>>> ['who', 'what', 'When', 'What']
re正則表達(dá)式模塊還包括一些有用的操作正則表達(dá)式的函數(shù)。
**下面主要介紹match函數(shù)以及search函數(shù)。
re.match 嘗試從字符串的開始匹配一個(gè)模式。
re.match(pattern, string, flags)
pattern是正則表達(dá)式,如果匹配成功,則返回一個(gè)Match,否則返回一個(gè)None;
string表示要匹配的字符串;
flags是標(biāo)致位,用于控制正則表達(dá)式的匹配方式,如:是否區(qū)分大小寫,多行匹配等等。
函數(shù)的返回值為真或者假。
例如:match(‘p’,’python’)返回值為真;match(‘p’,’www.python.org’)返回值為假。
re.match的例子1
import re
a=re.match("wh","What are you doing? who is you mate?",re.I)#re.I忽略大小寫
wh
if a:
print "you are my angle"
else:
print "i lose you "
re.search會(huì)在給定字符串中尋找第一個(gè)匹配給定正則表達(dá)式的子字符串。
函數(shù)的返回值:如果查找到則返回查找到的值,否則返回為None。
原型:
re.search(pattern, string, flags)
每個(gè)參數(shù)的含意與re.match一樣。
import re
content="What are you doing? who is your mate?"
regu_cont=re.compile("\wwh\w",re.I)
yl=regu_cont.match(content)
if yl:
print yl.group(0)
else:
print "what happen?"
解析:首先創(chuàng)造了需要正則表達(dá)式匹配的字符串content;
接著利用re.compile()來(lái)創(chuàng)建了我們所需要的匹配規(guī)則,創(chuàng)建了模式對(duì)象regu_cont;
yl用來(lái)接收對(duì)內(nèi)容content字符串進(jìn)行regu_cont正則表達(dá)式實(shí)現(xiàn)match函數(shù)的結(jié)果
如果有yl不為空,則使用m.group(index)輸出查找到的子字符串
否則(返回值為None) print “what happen?”
match例子2 </pre>
<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;">
'''
match如果查找到結(jié)果, 將返回一個(gè) MatchObject,你可以查詢 MatchObject 關(guān)于匹配字符串的相關(guān)信息了。MatchObject 實(shí)例也有幾個(gè)方法和屬性;最重要的那些如下所示: group() 返回被 RE 匹配的字符串
start() 返回匹配開始的位置
end() 返回匹配結(jié)束的位置
span() 返回一個(gè)元組包含匹配 (開始,結(jié)束) 的位置
'''
import re
content="What are you doing? who is your mate?"
regu_cont=re.compile("\wwh\w",re.I)
yl=regu_cont.match(content)
if yl:
print yl.group(0)
else:
print "pass the test"
print yl.group()
print yl.start()
print yl.end()
print yl.span()
執(zhí)行結(jié)果為:
What
What
0
4
(0, 4)</pre>
<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;"> #search()方法與match()方法類似 </pre>
import re
content='Where are you from? You look so hansome.'
regex=re.compile(r'\w*som\w*')
m=regex.search(content)
if m:
print m.group(0)
else:
print "Not found"
<pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word;">#相當(dāng)于:
import re
m=re.search(r'\wsom\w','Where are you from? You look so handsome.',re.I)
if m:
print m.group(0)
else:
print "not found"</pre>