Python 自1.5版本起增長了
re
模塊,它提供 Perl 風格的正則表達式模式。python
re
模塊使 Python 語言擁有所有的正則表達式功能。正則表達式
參數含義函數
- pattern: 字符串形式的正則表達式
- string: 要匹配的字符串
- flags: 可選,表示匹配模式
- pos:可選,字符串中開始搜索的位置索引
- endpos:可選,endpos 限定了字符串搜索的結束
- 不填pos endpos默認掃描所有
compile(pattern, flags=0)spa
match()
等函數>>> test = '1 one 2 two 3 three' >>> a=re.compile(r'\d+') >>> b=a.match(test) >>> print(f"輸出:{b[0]}") 輸出:1
re.match(pattern, string, flags=0)code
Pattern.match(string, pos, endpos)對象
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.start(group), m.end(group))
注意若是 group
沒有在這個匹配中,就返回 (-1, -1)
索引
re.search(pattern, string, flags=0)three
Pattern.search(string, pos, endpos)rem
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()
只匹配字符串的開始,若是字符串開始不符合正則表達式,則匹配失敗,函數返回 Nonesearch()
匹配整個字符串,直到找到一個匹配re.findall(pattern, string, flags=0)字符串
Pattern.findall(string, pos, endpos)
>>> 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(pattern, string, flags=0)
Pattern.finditer(string, pos, endpos)
>>> 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(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(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(pattern, string, maxsplit=0, flags=0)
maxsplit:表示分割次數,默認爲0,表示無限制
>>> 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 該標誌經過給予你更靈活的格式以便你將正則表達式寫得更易於理解。