正規表示式是乙個特殊的字串行,它能幫助你方便的檢查乙個字串是否與某種模式匹配。
語法描述
表示式示例
匹配示例
字元普通字元
匹配自身
abcabc
\t匹配乙個製表符
\r匹配乙個回車符
\n匹配乙個換行符
\f匹配乙個換頁符
\v匹配乙個垂直製表符
.匹配任意除換行符'\n'外的字元
在dotall模式中也能匹配換行符
a.cabc
^匹配輸入字串的開始位置
$匹配輸入字串的結束位置
\b匹配乙個單詞邊界,即字與空格間的位置
\b匹配非單詞邊界
|指明兩項之間的選擇乙個
[...]
字符集,對應的位置可以是字符集中任意字元。
字符集中的字元可以逐個列出,也可以給出範圍,例如 [abc]或者[a-c]。
第乙個字元如果是 ^ 則表示取反,如 [^abc]表示除abc外的任意字元。
所有的特殊字元在字符集中都失去原有的特殊含義。在字符集中如果要使用 ]、- 、^,可以在前面加上反斜槓,或把 ] 、- 放在第乙個字元,把 ^ 放在非第乙個字元。
aa-z\[\-^]c
a[ca]c
abca-c
a^c預定義字符集
\d數字:[0-9]
a\dc
a6c\d
非數字:[^0-9] 或者[^\d]
a\dc
abc\s
空白字元:[空格\t\r\n\f\v]
a\sc
a c\s
非空白字元:[^\s]
a\sc
abc\w
單詞字元:[a-za-z0-9_]
a\wc
a_c\w
非單詞字元:[^\w]
a\wc
a c數量詞
*匹配前乙個字元任意次(0次、1次、...)
abc*d
abdabccd
+匹配前乙個字元1次或多次
abc+d
abcd
abccd
?匹配前乙個字元0次或1次
abc?d
abdabcd
匹配前乙個字元n次
匹配前乙個字元至少n次
匹配前乙個字元至少n次,最多m次
abcd
abcccd
注意:*、+限定符都是貪婪的,因為它們會盡可能多的匹配文字,只有在它們的後面加上乙個?就可以實現非貪婪或最小匹配。
python 自1.5版本起增加了re 模組,使得 python 語言擁有全部的正規表示式功能,常用正規表示式的方法有如下幾個:
compile方法中的 flags 引數用於控制正規表示式的匹配方式,如:是否區分大小寫,多行匹配等等。
re.i
使匹配對大小寫不敏感
re.l
做本地化識別(locale-aware)匹配
re.m
多行匹配,影響 ^ 和 $
re.s
使 . 匹配包括換行在內的所有字元
re.u
根據unicode字符集解析字元。這個標誌影響 \w, \w, \b, \b.
re.x
該標誌通過給予你更靈活的格式以便你將正規表示式寫得更易於理解。
match方法嘗試從字串的起始位置與模式進行匹配,如果匹配成功的話,會返回乙個包含了所有匹配分組的結果物件,否則返回none。
import re
match_str = 'cats are smarter than dogs'
match_pattern = re.compile(r'(.*) are (.*?) .*', re.m | re.i)
match_ret = match_pattern.match(match_str)
if match_ret:
print("match_ret.group(0) : ", match_ret.group(0))
print("match_ret.group(1) : ", match_ret.group(1))
print("match_ret.group(2) : ", match_ret.group(2))
else:
print("no match!!")
print(match_ret.groups())
程式列印結果:
match_ret.group(0) : cats are smarter than dogs
match_ret.group(1) : cats
match_ret.group(2) : smarter
('cats', 'smarter')
search方法會從左至右掃瞄整個字串並返回第乙個與模式匹配成功的部分字串。
import re
search_str = 'big fish eat small fish, small fish eat shrimp'
search_pattern = re.compile(r'(.*) fish .*', re.m | re.i)
search_ret = search_pattern.match(search_str)
if search_ret:
print("search_ret.group(0) : ", search_ret.group(0))
print("search_ret.group(1) : ", search_ret.group(1))
else:
print("no search!!")
程式列印結果:
search_ret.group(0) : big fish eat small fish, small fish eat shrimp
search_ret.group(1) : big fish eat small fish, small
findall方法會在字串中找到與模式匹配的所有子串,並返回乙個列表,如果沒有找到匹配的字串,則返回空列表。
import re
find_str = 'big fish eat small fish, small fish eat shrimp'
find_pattern = re.compile(r'(\s* fish)', re.m | re.i)
find_ret = find_pattern.findall(find_str)
print(find_ret)
程式列印結果:
['big fish', 'small fish', 'small fish']
另外還有乙個finditer方法和findall很類似,也是在字串中找到正規表示式所匹配的所有子串,但是會把它們作為乙個迭代器返回。
find_str = 'big fish eat small fish, small fish eat shrimp'
find_pattern = re.compile(r'(\s* fish)', re.m | re.i)
iter = find_pattern.finditer(find_str)
for match in iter:
print(match.group())
sub方法用來使用正規表示式對與模式匹配的子串進行替換,可以通過count引數來指定替換次數。
import re
sub_str = 'big fish eat small fish, small fish eat shrimp'
sub_pattern = re.compile(r'(\s* fish)', re.m | re.i)
sub_ret = sub_pattern.sub('fish', sub_str, count=3)
print(sub_ret)
程式列印結果:
fish eat fish, fish eat shrimp
與sub方法類似地,有乙個split方法能夠利用與正則模式匹配的子串將字串進行分割並將分割後的結果放入列表返回。
split_str = 'big fish eat small fish, small fish eat shrimp'
split_pattern = re.compile(r'(\s* fish)', re.m | re.i)
split_ret = split_pattern.split(sub_str,maxsplit=0)
print(split_ret)
程式列印結果:
['', 'big fish', ' eat ', 'small fish', ', ', 'small fish', ' eat shrimp']
Python中使用正規表示式
python unix與linux系統管理指南 學習筆記 python中使用正規表示式,應該要養成建立編譯後的正規表示式的習慣,使用方法如下 usr bin env python import re def run re pattern error re obj re.compile pattern...
Python中使用正規表示式
本文通過示例來描述如何在python中使用正規表示式來統計文字中的所有數字。示例中的文字來自命令列的管道資料,sys.stdin.readlines 主要是因為作者需要在命令列的輸出資訊中做數字統計。示例 1,列出根目錄下所有檔案或資料夾的名稱字串中包含的數字 import re for name ...
Python中使用正規表示式
本文通過示例來描述如何在python中使用正規表示式來統計文字中的所有數字。示例中的文字來自命令列的管道資料,python view plain copy sys.stdin.readlines 主要是因為作者需要在命令列的輸出資訊中做數字統計。示例 1,列出根目錄下所有檔案或資料夾的名稱字串中包含...