python正則表達式——經常使用函數

re.compile()

python re模塊提供了一個正則表達式引擎接口,能夠將REString編譯成對象,並用編譯好的對象來匹配。若是一個正則表達式常常用來作匹配,那麼能夠編譯,這樣速度更快。python

  • 基本用法以下:
>>> import re
>>> p = re.compile("c[a-g]t")
>>> print(p)
<_sre.SRE_Pattern object at 0x11e6420>
>>> p.findall("cat cbtt")
['cat', 'cbt']
  • 能夠接受可選標誌參數,實現不一樣的特殊功能和語法變動。

re.I 不區分大小寫 ......正則表達式

>>> p = re.compile("c[a-g]t", re.I)
>>> p.findall("cat cBTt")
['cat', 'cBT']
  • 反斜槓

re.match()

若RE在字符串開始的位置匹配,則返回一個'matchObject'實例(對象);不然返回None。 一般用法是將match的返回值賦給一個變量,而後判斷這個變量是否在None。 固然,返回的matchObject也有一些類方法,這裏暫時省略,之後補充。app

>>> p = re.compile("abc")
>>> mo = p.match("aaaaabcdrfg")
>>> p = re.compile("abc")
>>> mo1 = p.match("aaaaabcdrfg")
>>> mo2 = p.match("abcdrfg")
>>> print(mo1) #RE沒有出如今字符串的開頭,所以爲None
None
>>> print(mo2)
<_sre.SRE_Match object at 0x12425e0>
>>> mo3 = p.search("aaaaabcdrfg")
>>> mo4 = p.search("abcdrfg")
>>> print(mo3)
<_sre.SRE_Match object at 0x1309b28>
>>> print(mo4)
<_sre.SRE_Match object at 0x1309b90>

re.search()

掃描字符串,找到RE匹配的位置,成功則返回一個'matchObject'實例(對象);不然返回None。函數

re.findall()

找到RE匹配的全部子串,並把他們做爲一個列表返回。code

re.finditer()

找到RE匹配的全部子串,並把他們做爲一個迭代器返回。對象

re.sub()

sub(pattern, repl, string, count=0, flags=0) Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used.接口

>>> re.sub(r"a", "b", "haha") # count=0或者省略表示所有替換
'hbhb'
>>> re.sub(r"a", "b", "haha", 0)
'hbhb'
>>> re.sub(r"a", "b", "haha", 1)# count=1表示所有替換1次
'hbha'

這裏與字符串函數replace區別就是,re.sub()的pattern支持正則表達式,使用更加靈活ci

re.subn()

subn(pattern, repl, string, count=0, flags=0) Return a 2-tuple containing (new_string, number). new_string is the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in the source string by the replacement repl. number is the number of substitutions that were made. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used.字符串

>>> re.subn(r"a", "b", "haha") #參數與sub同樣
('hbhb', 2)
>>> re.subn(r"a", "b", "haha", 1)
('hbha', 1)
>>> re.subn(r"a", "b", "haha", 2)
('hbhb', 2)

返回一個兩個元素的元組,第一個元素表示替換後的結果,第二個元素表示替換的次數。string

re.split()

split(pattern, string, maxsplit=0, flags=0) Split the source string by the occurrences of the pattern, returning a list containing the resulting substrings. 這個函數與字符串的split區別就是這裏的pattern支持正則表達式,使用更加靈活。

>>> re.split(r"[a-f]", "afternoon")
['', '', 't', 'rnoon']
相關文章
相關標籤/搜索