常用正则表达式的方法:
re.compile(编译)
pattern.match(从头找一个)
pattern.search(找一个)
pattern.findall(找所有)
pattern.sub(替换)
In [5]: re.findall('.',' ') Out[5]: [] In [6]: re.findall('.',' ',re.DOTALL) Out[6]: [' '] In [7]: re.findall('.',' ',re.S) Out[7]: [' '] In [8]: re.findall('a[bcd]e','abce') Out[8]: [] In [9]: re.findall('a[bcd]+e','abce') Out[9]: ['abce']
In [7]: import re In [8]: b = 'hahazou1kan2' In [9]: re.sub('d','_',b) Out[9]: 'hahazou_kan_' In [10]: re.sub('d','',b) Out[10]: 'hahazoukan' In [11]: re.compile('d') Out[11]: re.compile(r'd', re.UNICODE) In [12]: p = re.compile('d') In [13]: p.findall(b) Out[13]: ['1', '2']
In [16]: a = 'a b' In [17]: a[1] Out[17]: ' ' In [18]: len(a) Out[18]: 3 In [19]: b = r'a b' In [20]: b[1] Out[20]: '\' In [21]: len(b) Out[21]: 4
---------------------------
In [23]: r'a b' == 'a b' Out[23]: False In [24]: re.findall('a b','a b') Out[24]: ['a b'] In [25]: r'a b' == 'a\nb' Out[25]: True In [26]: re.findall(r'a b','a\nb') Out[26]: [] In [27]: re.findall(r'a b','a b') Out[27]: ['a b'] In [28]: re.findall(r'a\nb','a\nb') Out[28]: ['a\nb'] In [29]: re.findall(r'a\ b','a\ b') Out[29]: ['a\ b']