正規表示式

2021-09-29 03:41:55 字數 2968 閱讀 5367

正規表示式是對字串操作的一種邏輯公式,就是用實現定義好的一些特定字元、及這些特定字元的組合,組成乙個「規則字串」,這個「規則字串」用來表達對字串的一種過濾邏輯。

不是python所獨有,需要通過re模組載入。

正規表示式測試**:開源中國

常用匹配字元:

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

re.match(patter,string,flags=0)
例項

import re

content = 'hello 123 4537 world this is regex demo'

print(len(content))

result = re.match('^hello\s\d\d\d\s\d\s.*demo$',content)

print(result)

print(result.group()) # 匹配的結果

print(result.span()) # 匹配字元的長度

import re

content = 'hello 123 4537 world_this is regex demo'

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

print(result)

import re

content = 'hello 1234537 world_this is regex demo'

result = re.match('^hello\s(\d+)\sworld.*demo$',content)

print(result.group(1))

import re

content = 'hello 1234537 world_this is regex demo'

result = re.match('^he.*(\d+).*demo$',content)

#這裡小括號內只會匹配到7

import re

content = 'hello 1234537 world_this is regex demo'

result = re.match('he.*?(\d+).*demo$',content)

# 這裡會匹配完整的數字

import re

content = ''''''hello 1234537 world_this

is regex demo''''''

result1 = re.match('he.*?(\d+).*demo$',content)

result2 = re.match('he.*?(\d+).*demo$',content,re.s)

import re

content="this is $5.00"

result1 = re.match("this is $5.00",content)

result2 = re.match("this is \$5\.00)

小結:盡量使用泛匹配,使用括號得到匹配目標,盡量使用非貪婪模式,有換行符就用re.s,有特殊字元用轉義

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

import re

content = "extra strings hello 1234657 world_this is a regex demo extra strings"

result_re_match = re.match("he.*?(\d+).*demo$",conent)

resutl_re_search = re.search("he.*?(\d+).*demo$",content)

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

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

小括號既可以作為整體,又可以作為分組。

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

import re

content = "extra strings hello 1234657 world_this is a regex demo extra strings"

# 替換

content1 = re.sub('\d+','',content)

content2 = re.sub('\d+','replacement',content)

# 增加內容

content3 = re.sub('(\d+),r'\1 8910',content)

可以採用sub先進行替換,然後再用findall。

ps:使用strip()方法去除空格

將正規表示式編譯成正規表示式物件,以便復用匹配模式。

import re

content = '''hello 1234567 world_this

is a regex demo'''

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

result = re.match(pattern,content)

print(result)

正規表示式 正規表示式 總結

非負整數 d 正整數 0 9 1 9 0 9 非正整數 d 0 負整數 0 9 1 9 0 9 整數 d 非負浮點數 d d 正浮點數 0 9 0 9 1 9 0 9 0 9 1 9 0 9 0 9 0 9 1 9 0 9 非正浮點數 d d 0 0 負浮點數 正浮點數正則式 英文本串 a za z...

正規表示式 表示式

網域名稱 a za z0 9 a za z0 9 a za z0 9 a za z0 9 interneturl a za z s 或 http w w w 手機號碼 13 0 9 14 5 7 15 0 1 2 3 5 6 7 8 9 18 0 1 2 3 5 6 7 8 9 d 號碼 x x x...

Linux正規表示式 編寫正規表示式

為了所有實用化的用途,你可以通過使用程式產生正確的結果。然而,並不意味著程式總是如你所願的那樣正確地工作。多數情況下,如果程式不能產生想要的輸出,可以斷定真正的問題 排除輸入或語法錯誤 在於如何描述想要的東西。換句話說,應該考慮糾正問題的地方是描述想要的結果的表示式。表示式不完整或者公式表示得不正確...