一、re.match
re.match 嘗試從字符串的開始匹配一個模式,如:下面的例子匹配第一個單詞。
import re
text = "JGood is a handsome boy, he is cool, clever, and so on..."
m = re.match(r"(\w+)\s", text)
if m:
print m.group(0), '\n', m.group(1)
else:
print 'not match'
re.match的函數原型爲:re.match(pattern, string, flags)
第一個參數是正則表達式,這裏爲"(\w+)\s",如果匹配成功,則返回一個Match,否則返回一個None;
第二個參數表示要匹配的字符串;
第三個參數是標緻位,用於控制正則表達式的匹配方式,如:是否區分大小寫,多行匹配等等。
二、re.search
re.search函數會在字符串內查找模式匹配,只到找到第一個匹配然後返回,如果字符串沒有匹配,則返回None。
import re
text = "JGood is a handsome boy, he is cool, clever, and so on..."
m = re.search(r'\shan(ds)ome\s', text)
if m:
print m.group(0), m.group(1)
else:
print 'not search'
re.search的函數原型爲: re.search(pattern, string, flags)
每個參數的含意與re.match一樣。
re.match與re.search的區別:re.match只匹配字符串的開始,如果字符串開始不符合正則表達式,則匹配失敗,函數返回None;而re.search匹配整個字符串,直到找到一個匹配。
三、re.sub
re.sub用於替換字符串中的匹配項。下面一個例子將字符串中的空格 ' ' 替換成 '-' :
import re
text = "JGood is a handsome boy, he is cool, clever, and so on..."
print re.sub(r'\s+', '-', text)
re.sub的函數原型爲:re.sub(pattern, repl, string, count)
其中第二個函數是替換後的字符串;本例中爲'-'
第四個參數指替換個數。默認爲0,表示每個匹配項都替換。
re.sub還允許使用函數對匹配項的替換進行復雜的處理。如:re.sub(r'\s', lambda m: '[' + m.group(0) + ']', text, 0);將字符串中的空格' '替換爲'[ ]'。
四、re.split
可以使用re.split來分割字符串,如:re.split(r'\s+', text);將字符串按空格分割成一個單詞列表。
五、re.findall
re.findall可以獲取字符串中所有匹配的字符串。如:re.findall(r'\w*oo\w*', text);獲取字符串中,包含'oo'的所有單詞。
六、re.compile
可以把正則表達式編譯成一個正則表達式對象。可以把那些經常使用的正則表達式編譯成正則表達式對象,這樣可以提高一定的效率。下面是一個正則表達式對象的一個例子:
import re
text = "JGood is a handsome boy, he is cool, clever, and so on..."
regex = re.compile(r'\w*oo\w*')
print regex.findall(text) #查找所有包含'oo'的單詞
print regex.sub(lambda m: '[' + m.group(0) + ']', text) #將字符串中含有'oo'的單詞用[]括起來。
七、group()
1.group([group1,…])
返回匹配到的一個或者多個子組。如果是一個參數,那麼結果就是一個字符串,如果是多個參數,那麼結果就是一個參數一個item的元組。group1的 默認值爲0(將返回所有的匹配值).如果groupN參數爲0,相對應的返回值就是全部匹配的字符串,如果group1的值是[1…99]範圍之內的,那 麼將匹配對應括號組的字符串。如果組號是負的或者比pattern中定義的組號大,那麼將拋出IndexError異常。如果pattern沒有匹配到, 但是group匹配到了,那麼group的值也爲None。如果一個pattern可以匹配多個,那麼組對應的是樣式匹配的最後一個。另外,子組是根據括 號從左向右來進行區分的。
>>> m=re.match("(\w+) (\w+)","abcd efgh, chaj")
>>> m.group() # 匹配全部
'abcd efgh'
>>> m.group(1) # 第一個括號的子組.
'abcd'
>>> m.group(2)
'efgh'
>>> m.group(1,2) # 多個參數返回一個元組
('abcd', 'efgh')
>>> m=re.match("(?P<first_name>\w+) (?P<last_name>\w+)","sam lee")
>>> m.group("first_name") #使用group獲取含有name的子組
'sam'
>>> m.group("last_name")
'lee'
下面把括號去掉
>>> m=re.match("\w+ \w+","abcd efgh, chaj")
>>> m.group()
'abcd efgh'
>>> m.group(1)
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
m.group(1)
IndexError: no such group
If a group matches multiple times, only the last match is accessible:
如果一個組匹配多個,那麼僅僅返回匹配的最後一個的。
>>> m=re.match(r"(..)+","a1b2c3")
>>> m.group(1)
'c3'
>>> m.group()
'a1b2c3'
Group的默認值爲0,返回正則表達式pattern匹配到的字符串
>>> s="afkak1aafal12345adadsfa"
>>> pattern=r"(\d)\w+(\d{2})\w"
>>> m=re.match(pattern,s)
>>> print m
None
>>> m=re.search(pattern,s)
>>> m
<_sre.SRE_Match object at 0x00C2FDA0>
>>> m.group()
'1aafal12345a'
>>> m.group(1)
'1'
>>> m.group(2)
'45'
>>> m.group(1,2,0)
('1', '45', '1aafal12345a')
2.groups([default])
返回一個包含所有子組的元組。Default是用來設置沒有匹配到組的默認值的。Default默認是"None”,
>>> m=re.match("(\d+)\.(\d+)","23.123")
>>> m.groups()
('23', '123')
>>> m=re.match("(\d+)\.?(\d+)?","24") #這裏的第二個\d沒有匹配到,使用默認值"None"
>>> m.groups()
('24', None)
>>> m.groups("0")
('24', '0')
3.groupdict([default])
返回匹配到的所有命名子組的字典。Key是name值,value是匹配到的值。參數default是沒有匹配到的子組的默認值。這裏與groups()方法的參數是一樣的。默認值爲None
>>> m=re.match("(\w+) (\w+)","hello world")
>>> m.groupdict()
{}
>>> m=re.match("(?P<first>\w+) (?P<secode>\w+)","hello world")
>>> m.groupdict()
{'secode': 'world', 'first': 'hello'}
通過上例可以看出,groupdict()對沒有name的子組不起作用