正規表示式處理字元(Python)

2021-09-18 04:33:02 字數 3174 閱讀 5953

.

匹配任意乙個字元,除了換行符

*匹配0個或多個表示式

+匹配1個或多個表示式

.*匹配任意長度字串

\s匹配任意空白字元,等價於[\t\n\r\f]

\s匹配任意非空字元

\w匹配字母數字和下劃線

\d匹配任意數字

精確匹配n個前面表示式;如:\d

^匹配字串開頭

$匹配字串結尾

內建庫re

re.match()

re.match(pattern,str,flags=0)返回乙個match物件,判斷正規表示式和字串是否匹配

特別注意:pattern和str的第乙個字元必須匹配!

import re

content =

'hello 1234567 world_this is mine'

result = re.match(

'^hello\s\d\s.*$'

,content)

print

(len

(content)

)print

(result.group())

#返回匹配結果

print

(result.span())

#返回匹配範圍

result = re.match(

'^hello\s(\d+).*\s(\w+)$'

,content)

print

(result.group(1)

,result.group(2)

)#使用括號得到匹配目標

output:

32hello 1234567 world_this is mine

(0, 32)

1234567 mine

貪婪匹配

盡可能多的匹配字元

例子:.*

import re

content =

'hello 1234567 world_this is mine'

result = re.match(

'^h.*(\d+).*$'

,content)

print

(result.group(1)

)

output:

7

非貪婪匹配(優先使用)

盡可能少的匹配字元

例子:.*?

import re

content =

'hello 1234567 world_this is mine'

result = re.match(

'^h.*?(\d+).*$'

,content)

print

(result.group(1)

)

output:

1234567

匹配模式

有換行符須flags=re.s

import re

content =

'hello 1234567\nworld_this is mine'

result = re.match(

'^.*?(\w+)$'

,content,re.s)

#讓 . 能夠匹配換行符

print

(result.group(1)

)

output:

mine

re.search()

re.search(pattern, str, flags) 掃瞄整個字串並返回第乙個成功的匹配

re.search()與re.match()的區別僅在於:search無需從字串頭部開始匹配

為了方便,盡可能用search代替match

re.findall()

搜尋字串,以列表形式返回全部能匹配的子串
import re

content =

'noting 576 hello 1234567 world_this 321 is mine'

result = re.findall(

'\s(\d+)\s'

,content)

print

(result)

output:

['576', '1234567', '321']

re.sub()

re.sub(pattern, sub_str,str) 用sub_str替換掉能和pattern匹配的子串;返回替換後的字串
import re

content =

'noting hello 1234567 world_this is mine'

result = re.sub(

'\d+'

,'sub'

,content)

print

(result)

output:

noting hello sub world_this is mine

re.compile()

re.compile(pattern, flags)將乙個正規表示式編譯成正則物件,以便重複使用
import re

content =

'noting hello 123\n4567 world_this is mine'

pattern = re.

compile

('\d.*\d'

,re.s)

result = re.search(pattern,content)

print

(result.group(

))

python正規表示式元字元 正規表示式

字元 描述將下乙個字元標記為乙個特殊字元 或乙個原義字元 或乙個 向後引用 或乙個八進位制轉義符。例如,n 匹配字元 n n 匹配乙個換行符。序列 匹配 而 則匹配 匹配輸入字串的開始位置。如果設定了 regexp 物件的 multiline 屬性,也匹配 n 或 r 之後的位置。匹配輸入字串的結束...

python處理正規表示式

一.正規表示式 正規表示式是乙個用特殊符號表示的字串行,用來檢查乙個字串是否與某種字串模式匹配。較常用的正規表示式有 正規表示式 匹配物件 普通字元 匹配對應的字元 n匹配換行符 t匹配製表符 d匹配十進位制數字 d匹配除了十進位制數字的其他字元 w匹配字母,數字,下劃線 w匹配除了字母,數字,下劃...

正規表示式字元

正規表示式符號 字元 描述 標記下乙個字元是特殊字元或文字。例如,n 和字元 n 匹配。n 則和換行字元匹配。序列 和 匹配,而 則和 匹配。匹配輸入的開頭。匹配輸入的末尾。匹配前乙個字元零或多次。例如,zo 與 z 或 zoo 匹配。匹配前乙個字元一次或多次。例如,zo 與 zoo 匹配,但和 z...