日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3 re模块_Python3 正则表达式 re 模块的使用 - 学习笔记

發布時間:2024/1/23 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 re模块_Python3 正则表达式 re 模块的使用 - 学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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)

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}")

輸出:

輸出:

輸出:

區別

二者最大的區別在于一個返回列表,一個返回迭代器

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該標志通過給予你更靈活的格式以便你將正則表達式寫得更易于理解。

總結

以上是生活随笔為你收集整理的python3 re模块_Python3 正则表达式 re 模块的使用 - 学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。