Python程式設計 re正則庫基本使用

2021-08-19 11:35:45 字數 3769 閱讀 8645

之前的文章:

python程式設計:re正則庫

# 字符集

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

\w 匹配非字母數字及下劃線

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

\s 匹配任意非空字元

\d 匹配任意數字,等價於[0-

9]\d 匹配任意非數字

\a 匹配字串開始

\z 匹配字串結束,如果是換行,只匹配到換行前的結束字串

\z 匹配字串結束

\g 匹配最後匹配完成的位置

\n 匹配乙個換行符

\t 匹配乙個製表符[.

..] 匹配一組字元,單獨列出,[abc]匹配a,b或c[^

...] 匹配不在其中的字元

# 數量控制

a|b 匹配a或b

^ 匹配字串的開頭

$ 匹配字串的末尾

. 匹配任意字元,除了換行符, 指定re.dotall 匹配包括換行符的任意字元

* 零個或多個表示式

+ 乙個或多個表示式

? 零個或乙個表示式,非貪婪匹配

匹配n個表示式

匹配n~m次表示式,貪婪匹配

() 匹配括號內的表示式,表示組

import re

# match從頭開始匹配

# 常規匹配

content =

"fsadf5666asd"

ret = re.match(

"\w+"

, content)

print

(len

(content)

)print

(ret)

print

(ret.group())

print

(ret.span())

"""12

<_sre.sre_match object; span=(0, 12), match='fsadf5666asd'>

fsadf5666asd

(0, 12)

"""# 泛匹配

ret = re.match(

".*"

, content)

print

(ret)

# <_sre.sre_match object; span=(0, 8), match='fsadfasd'>

# 目標匹配

ret = re.match(

"[a-z]+(\d+)"

, content)

print

(ret)

print

(ret.group())

print

(ret.group(0)

)print

(ret.group(1)

)"""

<_sre.sre_match object; span=(0, 9), match='fsadf5666'>

fsadf5666

fsadf5666

5666

"""# 貪婪匹配

ret = re.match(

".*(\d+)"

, content)

print

(ret)

print

(ret.group(0)

)print

(ret.group(1)

)"""

<_sre.sre_match object; span=(0, 9), match='fsadf5666'>

fsadf5666

6"""

# 非貪婪匹配

ret = re.match(

".*?(\d+)"

, content)

print

(ret)

print

(ret.group(0)

)print

(ret.group(1)

)"""

<_sre.sre_match object; span=(0, 9), match='fsadf5666'>

fsadf5666

5666

"""# 匹配模式

content =

"""sadfasd34345sdfa

sdfasdf"""

# 匹配換行符

ret = re.match(

".*?(\d+).*"

, content, re.s)

print

(ret)

print

(ret.group(1)

)"""

<_sre.sre_match object; span=(0, 24), match='sadfasd34345sdfa\nsdfasdf'>

34345

"""# 轉義

content =

"sdfasdf$55.334"

# 匹配換行符

ret = re.match(

".*?(\$\d+\.\d+)"

, content, re.s)

print

(ret)

print

(ret.group(1)

)"""

<_sre.sre_match object; span=(0, 14), match='sdfasdf$55.334'>

$55.334

"""# 總結:

# 盡量使用泛匹配,使用括號得到匹配目標,

# 盡量使用非貪婪模式,有換行符就用re.s

# search掃瞄整個字串

# 能用search就不用match

content =

"fsadf5666asd"

ret = re.match(

"\d+"

, content)

print

(ret)

ret = re.search(

"\d+"

, content)

print

(ret)

"""none

<_sre.sre_match object; span=(5, 9), match='5666'>

"""# findall 找到所有

content =

"fsadf5666asd234"

ret = re.findall(

"\d+"

, content)

print

(ret)

# ['5666', '234']

# sub 替換字串

content =

"fsadf5666asd234"

ret = re.sub(

"\d+",""

, content)

print

(ret)

# fsadfasd

# compile 編譯成正則物件,便於復用

content =

"fsadf5666asd234"

pattern = re.

compile

("\d+"

)ret = pattern.findall(content)

print

(ret)

# ['5666', '234']

r''一般用在正規表示式中,稱為原始字串,

作用將python語法中的反斜槓轉義給取消,將其設定成為乙個普通的字串

python爬蟲 re庫(正則)

1.re.match re.match嘗試從字元創的起始位置匹配乙個模式,如果不是起始位置匹配成功的話,就會返回none。re.match pattern,string,flags 0 2.最常規的匹配 import re content hello 123 4567 world this is a...

正則re的基本用法

表示式 含義 預設匹配除 n之外的任意乙個字元,若指定flag dotall,則匹配任意字元,包括換行 從字元開頭開始匹配 匹配字元結尾 匹配號前的字元0次或多次,re.findall ab cabb3abcbbac 結果為 abb ab a 匹配前乙個字元1次或多次,re.findall ab a...

python正則re使用

1 import re 將正規表示式編譯成pattern物件 pattern re.compile r hello re.i 使用pattern匹配文字,獲得匹配結果,無法匹配時將返回none match pattern.match hello world if match 使用match獲得分組資...