本文不涉及正則表達式本身的內容,只記一下python中正則的用法及常用方法。
python中用re模塊進行正則處理
>>>import re>>>s = r'abc'>>>re.findall(s,'aaaabcaaaaa')['abc']
或先編譯(會更快):
>>> import re>>> r1 = re.compile(r'abc', re.I)>>> re.findall(r1,'ababcabd')['abc']#或用下面的方式>>>r1.findall('ababcabd')['abc']
常用編譯標志:

1、compile:對正則進行編譯,使運行速度加快,用法如上
2、match:決定RE是否在字符串剛開始的位置,返回的是Match object對象,沒匹配到返回None
3、search:掃描字符串,找到這個 RE 匹配的位置,返回的是Match object對象,沒匹配到返回None
4、findall:找到RE匹配的所有子串,作為列表返回
5、finditer:找到RE匹配的所有子串,作為迭代器返回,迭代器中是 Match object對象
6、sub(regex,replace【可以是函數】,content,count=0【可選,替換的數量,默認為全部】,flags=0【可選,正則參數,如re.I】):數據替換
詳細參考:http://www.crifan.com/python_re_sub_detailed_introduction/
7、subn(regex,replace,content,count=0,flags=0):跟sub相同,只是返回值中包含替換次數
8、split(string [, maxsplit = 0]):把字符串按正則分隔
1、group():返回被RE匹配的字符串
2、start():返回匹配的開始的位置
3、end():返回匹配結束的位置
4、span():返回一個元組包含匹配(開始和結束)的位置
示例:
import rem = re.match(r'(/w+) (/w+)(?P<sign>.*)', 'hello world!') PRint "m.string:", m.stringprint "m.re:", m.reprint "m.pos:", m.posprint "m.endpos:", m.endposprint "m.lastindex:", m.lastindexprint "m.lastgroup:", m.lastgroup print "m.group(1,2):", m.group(1, 2)print "m.groups():", m.groups()print "m.groupdict():", m.groupdict()print "m.start(2):", m.start(2)print "m.end(2):", m.end(2)print "m.span(2):", m.span(2)print r"m.expand(r'/2 /1/3'):", m.expand(r'/2 /1/3') ### output #### m.string: hello world!# m.re: <_sre.SRE_Pattern object at 0x016E1A38># m.pos: 0# m.endpos: 12# m.lastindex: 3# m.lastgroup: sign
# m.group(1,2): ('hello', 'world')# m.groups(): ('hello', 'world', '!')# m.groupdict(): {'sign': '!'}# m.start(2): 6# m.end(2): 11# m.span(2): (6, 11)# m.expand(r'/2 /1/3'): world hello!
新聞熱點
疑難解答