Python3 正規表示式

2021-09-22 14:01:54 字數 4253 閱讀 2558

常用的匹配模式

正規表示式是乙個特殊的字串行,它能幫助你方便的檢查乙個字串是否與某種模式匹配。

re 模組使 python 語言擁有全部的正規表示式功能。

re.match函式

re.match 嘗試從字串的起始位置匹配乙個模式,如果不是起始位置匹配成功的話,match()就返回none。

re.match(pattern, string, flags=0)
匹配成功re.match方法返回乙個匹配的物件,否則返回none。

我們可以使用group(num) 或 groups() 匹配物件函式來獲取匹配表示式

import re

str = 'hello 123 4567 world'

print(len(str)) #輸出str的長度

result = re.match('^hello\s\d\d\d\s\d\s\w\wrld$', str)

print(result)

print(result.group())

print(result.span()) #輸出匹配到的字串的位置

result =re.match('^hello\s(\d\d\d)\s\d\s(\w\wrld)$', str)

print(result.group(),'\ngroup(1)',result.group(1),'\ngroup(2)',result.group(2))#group(1)為正規表示式中第乙個括號內的內容

print(result.span())

result = re.match('^hello\s\d\d\d',str) #只匹配hello 123

print(result.group())

print(result.span())

執行結果

20

hello 123 4567 world

(0, 20)

hello 123 4567 world

group(1) 123

group(2) world

(0, 20)

hello 123

(0, 9)

import re

str = 'hello 123 666 world_this is a regex demo'

result = re.match('^hello.*demo$', str)

print(result)

print(result.group())

print(result.span())

result = re.match('^hello.*?6', str)

print('非貪婪方式匹配',result.group())

result = re.match('^hello.*6', str)

print('貪婪方式匹配',result.group())

其中

.*  表示任意匹配除換行符(\n、\r)之外的任何單個或多個字元

.*? 使用非貪婪方式匹配任意的字元

執行結果

hello 123 666 world_this is a regex demo

(0, 40)

非貪婪方式匹配 hello 123 6

貪婪方式匹配 hello 123 666

import re

str = 'hello 123 4567 world'

print(len(str)) #輸出str的長度

result = re.match('^hello\s\d\d\d\s\d\s\w\wrld$', str)

print(result)

print(result.group())

print(result.span()) #輸出匹配到的字串的位置

result =re.match('^hello\s(\d\d\d)\s\d\s(\w\wrld)$', str)

print(result.group(),'\ngroup(1)',result.group(1),'\ngroup(2)',result.group(2))#group(1)為正規表示式中第乙個括號內的內容

print(result.span())

執行結果

hello 123 4567 world 

group(1) 123

group(2) world

(0, 20)

總結:盡量使用泛匹配、使用括號得到匹配目標、盡量使用非貪婪模式、有換行符就用re.s

re.search函式

re.search 掃瞄整個字串並返回第乙個成功的匹配。

import re

str = 'extra stings hello 1234567 world_this is a regex demo extra stings'

result = re.search('.*?(\d+).*?demo', str)

#匹配多個數字

print(result)

print(result.group(1))

執行結果

1234567
總結:為匹配方便,能用search就不用match

re.findall函式

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

import re

str = '123 456 abc 789 ddd sd 12d'

results = re.findall('\d+ ', str)

#匹配多個數字

print(results)

for result in results :

print(result)

執行結果

['123 ', '456 ', '789 ']

123

456

789

re.sub函式

替換字串中每乙個匹配的子串後返回替換後的字串。

import re

str = '123 456 abc 789 ddd sd 12d'

result = re.sub('\d','0',str)

#把所有的數字替換成0

print(result)

執行結果

000 000 abc 000 ddd sd 00d
re.compile函式

將正則字串編譯成正規表示式物件。

將乙個正規表示式串編譯成正則物件,以便於復用該匹配模式。

import re

str = '''hello 1234567 world_this

is a regex demo'''

pattern = re.compile('hello.*demo', re.s)

#匹配的字串中存在換行符是使用re.s

result1 = re.match(pattern, str)

print('result1',result1)

result2 = re.match('hello.*demo', str, re.s)

print('result2',result2)

執行結果

result1 result2
正規表示式修飾符 - 可選標誌

修飾符描述

re.i

使匹配對大小寫不敏感

re.l

做本地化識別(locale-aware)匹配

re.m

多行匹配,影響 ^ 和 $

re.s

使 . 匹配包括換行在內的所有字元

re.u

根據unicode字符集解析字元。這個標誌影響 \w, \w, \b, \b.

re.x

該標誌通過給予你更靈活的格式以便你將正規表示式寫得更易於理解。

python3正規表示式

正規表示式,又稱規則表示式。英語 regular expression,在 中常簡寫為regex regexp或re 電腦科學的乙個概念。正規表示式通常被用來檢索 替換那些符合某個模式 規則 的文字。正規表示式是對字串操作的一種邏輯公式,就是用事先定義好的一些特定字元 及這些特定字元的組合,組成乙個...

Python3 正規表示式

正規表示式是乙個特殊的字串行,它能幫助你方便的檢查乙個字串是否與某種模式匹配。python 自1.5版本起增加了re 模組,它提供 perl 風格的正規表示式模式。re 模組使 python 語言擁有全部的正規表示式功能。compile 函式根據乙個模式字串和可選的標誌引數生成乙個正規表示式物件。該...

Python3 正規表示式

目錄 python 正規表示式 正規表示式基本知識 re模組 匹配字串 切分字串 分組 貪婪匹配 編譯 d表示匹配乙個數字,即0 9 w表示匹配乙個字母或數字 表示匹配任意乙個字元 表示匹配任意個字元 包括0個 或其前面的表示式出現任意次數 表示至少匹配乙個字元,或其前面的表示式至少出現1次 表示匹...