re 模塊的引入
Python 自1.5版本起增加了
re
模塊,它提供 Perl 風格的正則表達式模式。
re
模塊使 Python 語言擁有全部的正則表達式功能。
re 模塊的使用
參數含義
- pattern: 字符串形式的正則表達式
- string: 要匹配的字符串
- flags: 可選,表示匹配模式
- pos:可選,字符串中開始搜索的位置索引
- endpos:可選,endpos 限定了字符串搜索的結束
- 不填pos endpos默認掃描全部
re.compile()
compile(pattern, flags=0)
- 將正則表達式的樣式編譯為一個 正則表達式對象 (正則對象)
- 可以使用正則對象調用
match()
等函數
>>> test = '1 one 2 two 3 three'
>>> a=re.compile(r'\d+')
>>> b=a.match(test)
>>> print(f"輸出:{b[0]}")
輸出:1
re.match()與re.search()
re.match
re.match(pattern, string, flags=0)
Pattern.match(string, pos, endpos)
- 如果 string 的 開始位置 能夠找到這個正則樣式的任意個匹配,就返回一個相應的 匹配對象。如果不匹配,就返回 None
- 可以使用
group(num)
或groups()
匹配對象函數來獲取匹配表達式group(num=0)
表示匹配的整個表達式的字符串group()
可以一次輸入多個組號,在這種情況下它將返回一個包含那些組所對應值的元組。groups()
返回一個包含所有小組字符串的元組,從 1 到 所含的小組號。
>>> test = '1 one 2 two 3 three'
>>> a=re.compile(r'(\d+) (\w+)')
>>> b=a.match(test)
>>> print(f"輸出:{b.group()}")
>>> print(f"輸出:{b.group(2)}")
>>> print(f"輸出:{b.group(1,2)}")
>>> print(f"輸出:{b.groups()}")
輸出:1 one
輸出:one
輸出:('1', 'one')
輸出:('1', 'one')
Match.start([group])
和Match.end([group])
- 返回
group
匹配到的字串的開始和結束標號。 - 如果
group
存在,但未產生匹配,就返回-1
。
- 返回
Match.span([group])
- 對於一個匹配 m ,返回一個二元組
(m.start(group), m.end(group))
- 注意如果
group
沒有在這個匹配中,就返回(-1, -1)
- 對於一個匹配 m ,返回一個二元組
re.search()
re.search(pattern, string, flags=0)
Pattern.search(string, pos, endpos)
- 掃描整個 string 尋找第一個匹配的位置, 並返回一個相應的 匹配對象。如果沒有匹配,就返回 None
- 其他與
match()
一致
>>> test = 'one 2 two 3 three'
>>> a = re.compile(r'(\d+) (\w+)')
>>> b = a.search(test)
>>> c = a.match(test)
>>> print(c)
>>> print(f"輸出:{b.group()}")
>>> print(f"輸出:{b.group(2)}")
>>> print(f"輸出:{b.group(1,2)}")
>>> print(f"輸出:{b.groups()}")
輸出:None
輸出:2 two
輸出:two
輸出:('2', 'two')
輸出:('2', 'two')
區別
match()
只匹配字符串的開始,如果字符串開始不符合正則表達式,則匹配失敗,函數返回 None- 而
search()
匹配整個字符串,直到找到一個匹配
re.findall()與re.finditer()
re.findall()
re.findall(pattern, string, flags=0)
Pattern.findall(string, pos, endpos)
- 對 string 返回一個不重復的 pattern 的匹配列表, string 從左到右進行掃描,匹配按找到的順序返回
- 如果樣式里存在一到多個組,就返回一個組合列表;就是一個元組的列表(如果樣式里有超過一個組合的話)
>>> test = 'one 2 two 3 three'
>>> a = re.compile(r'(\d+) (\w+)')
>>> b = a.search(test)
>>> b=a.findall(test)
>>> print(f"輸出:{b}")
輸出:[('2', 'two'), ('3', 'three')]
re.finditer()
re.finditer(pattern, string, flags=0)
Pattern.finditer(string, pos, endpos)
- pattern 在 string 里所有的非重復匹配,返回為一個迭代器 iterator 保存了 匹配對象
>>> test = 'one 2 two 3 three'
>>> a = re.compile(r'(\d+) (\w+)')
>>> b = a.finditer(test)
>>> print(f"輸出:{b}")
>>> for i in b:
print(f"輸出:{i}")
輸出:<callable_iterator object at 0x036E7BD0>
輸出:<re.Match object; span=(4, 9), match='2 two'>
輸出:<re.Match object; span=(10, 17), match='3 three'>
區別
- 二者最大的區別在於一個返回列表,一個返回迭代器
re.sub()與re.subn()
re.sub()
re.sub(pattern, repl, string, count=0, flags=0)
- repl : 替換的字符串,也可為一個函數。
- count : 模式匹配后替換的最大次數,默認 0 表示替換所有的匹配。
- 最后返回替換結果
>>> test = '1 one 2 two 3 three'
>>> a=re.sub(r'(\d+)','xxx',test)
>>> print(f"輸出:{a}")
>>> print(f"輸出:{test}")
輸出:xxx one xxx two xxx three
輸出:1 one 2 two 3 three
re.subn()
re.subn(pattern, repl, string, count=0, flags=0)
參數含義同上
- 功能與
re.subn
相同,但是返回一個元組 (字符串, 替換次數)
>>> test = '1 one 2 two 3 three'
>>> a=re.subn(r'(\d+)','xxx',test)
>>> print(f"輸出:{a}")
>>> print(f"輸出:{test}")
輸出:('xxx one xxx two xxx three', 3)
輸出:1 one 2 two 3 three
re.split()
re.split(pattern, string, maxsplit=0, flags=0)
maxsplit:表示分割次數,默認為0,表示無限制
- 用 pattern 分開 string
- 如果在 pattern 中捕獲到括號,那么所有的組里的文字也會包含在列表里
>>> test = '1 one 2 two 3 three'
>>> a = re.split(r'\d+', test)
>>> b = re.split(r'(\d+)', test)
>>> print(f"輸出:{a}")
>>> print(f"輸出:{b}")
輸出:['', ' one ', ' two ', ' three']
輸出:['', '1', ' one ', '2', ' two ', '3', ' three']
正則表達式修飾符(匹配模式)
re.I 使匹配對大小寫不敏感
re.L 做本地化識別匹配
re.M 多行匹配,影響 ^ 和 $
遇到\n視為新的一行,重新匹配 ^ 和 $
re.S 使 . 匹配包括換行在內的所有字符
re.U 根據Unicode字符集解析字符。這個標志影響 \w, \W, \b, \B.
re.X 該標志通過給予你更靈活的格式以便你將正則表達式寫得更易於理解。