python類庫31[正則表達式]
一 re.search 和 re.match
python提供了2中主要的正則表達式操作:re.match 和 re.search。
match :只從字符串的開始與正則表達式匹配,匹配成功返回matchobject,否則返回None;
search :將字符串的所有字串嘗試與正則表達式匹配,如果所有的字串都沒有匹配成功,返回None,否則返回matchobject;(re.search相當于perl中的默認行為)
實例代碼:
def TestSearchAndMatch():
s1="HelloWorld, I am 30 !"
w1 = "World"
m1 = re.search(w1, s1)
if m1:
print("Find : %s" % m1.group())
if re.match(w1, s1) == None:
print("Cannot match")
w2 = "HelloWorld"
m2 = re.match(w2, s1)
if m2:
print("match : %s" % m2.group())
TestSearchAndMatch()
#Find : World
#Cannot match
#match : HelloWorld
二 re.compile 和 re.ignorecase
re.compile返回regrexobject對象, 用來重復使用regrexobject;
re.ignorecase用來在匹配時忽略大小寫;
實例代碼:
regex = "\d{3}-\d{7}"
regexobject = re.compile(regex)
print(regexobject.search("AAA 027-4567892").group())
print(regexobject.search("BBB 021-1234567").group())
print(regexobject.search("CCC 010-123456"))
TestCompile()
#027-4567892
#021-1234567
#None
def TestIgnorecase():
print(re.search('world', "hello world !").group())
print(re.search('World', "hello world !", re.IGNORECASE).group())
print(re.search('World', "hello world !"))
TestIgnorecase()
#world
#world
#None
三 matchobject
matchobject為re.search,re.match等匹配成功后返回的對象,包含了匹配的結(jié)果。
在正則表達式中,可以使用()來將部分正則表達式分組且編號,編號從1開始,使用\數(shù)字來使用,例如\1 \2 \3,(?P<name>)還可以給分組命名, 使用(?P=name)來使用命名的組。
matchobject.group()包含了所有匹配的內(nèi)容,等價于matchobject.group(0),此時的0表示所有的匹配;
matchobject.groups()包含了正則表達式中所有使用()定義的組對應(yīng)的匹配內(nèi)容;
matchobject.group(n),表示返回正則表達式中的第n個組()匹配的內(nèi)容,此時n不為0, 等價于matchobject.groups()[n-1];
matchobject.lastindex, 表示正則表達式中分組()的個數(shù);
實例代碼:
m = re.match(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<date>\d{2})", "2010-10-01, i am very happy")
print(m.group())
print(m.group(0))
print(m.groups())
print(m.group(1))
print(m.groups()[0])
print(m.group(2))
print(m.groups()[1])
print(m.group(3))
print(m.groups()[2])
print(m.groupdict())
print(m.lastindex)
TestMatchObject()
#2010-10-01
#2010-10-01
#('2010', '10', '01')
#2010
#2010
#10
#10
#01
#01
#{'date': '01', 'year': '2010', 'month': '10'}
#3
四 re和matchobject的方法split+findall+finditer+sub
split方法,使用給定的表達式來分割字符串;
findall方法,返回所有的與給定的表達式匹配的一個list;
finditer方法,返回所有與給定的表達式匹配的matchobject的iterator;
sub方法,使用新的字符串替換表達式匹配的字符串;
實例代碼:
#split findall finditer sub
s1 = "I am working in Microsoft !"
l = re.split('\s+', s1) # l is list type
print( l)
s2 = "aa 12 bb 3 cc 45 dd 88 gg 89"
l2 = re.findall('\d+', s2)
print(l2)
it = re.finditer('\d+', s2) # it is one iterator type
for i in it: # i is matchobject type
print (i.group())
s3 = re.sub('\d+', '200', s2)
print(s3)
TestReAndMatchObjectMethonds()
#['I', 'am', 'working', 'in', 'Microsoft', '!']
#['12', '3', '45', '88', '89']
#12
#3
#45
#88
#89
#aa 200 bb 200 cc 200 dd 200 gg 200
五 re.search只返回第一個匹配
實例代碼:
s1 = "bb 3 cc 45 dd 88 gg 89"
m = re.search('\d+', s1)
print(m.group())
TestSearch()
#3
六 非貪婪模式,單行多行模式
1 非貪婪flag (對貪婪標志*和+使用?)
>>> re.findall(r"a(\d+?)", "a23b")
['2']
>>> re.findall(r"a(\d+)", "a23b")
['23']
注意比較這種情況:
>>> re.findall(r"a(\d+)b", "a23b")
['23']
>>> re.findall(r"a(\d+?)b", "a23b")
['23']
2 如果你要多行匹配,那么加上re.S和re.M標志
re.S:.將會匹配換行符,默認.不會匹配換行符
>>> re.findall(r"a(\d+)b.+a(\d+)b", "a23b\na34b")
[]
>>> re.findall(r"a(\d+)b.+a(\d+)b", "a23b\na34b", re.S)
[('23', '34')]
>>>
re.M:^$標志將會匹配每一行,默認^和$只會匹配第一行
>>> re.findall(r"^a(\d+)b", "a23b\na34b")
['23']
>>> re.findall(r"^a(\d+)b", "a23b\na34b", re.M)
['23', '34']
多行查詢的實例:
flist = r.findall(fstr)
完!


浙公網(wǎng)安備 33010602011771號