正則表達式匹配的幾個步驟:
1. 用 import re 導入正則表達式模塊。
2.用 re.compile()函數創建一個 Regex 對象(記得使用原始字符串)。
3.向 Regex 對象的 search()方法傳入想查找的字符串。它返回一個 Match 對象。
4.調用 Match 對象的 group()方法,返回實際匹配文本的字符串。
正則表達式匹配更多模式:
1. 利用括號分組:group() 和groups() 方法。
>>> phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)') >>> mo = phoneNumRegex.search('My number is 415-555-4242.') >>> mo.group(1) '415 >>> mo.group(2) '555-4242' >>> mo.group(0) '415-555-4242' >>> mo.group() '415-555-4242' >>> mo.groups() ('415', '555-4242') >>> areaCode, mainNumber = mo.groups() >>> print(areaCode) 415 >>> print(mainNumber) 555-4242
>>> phoneNumRegex = re.compile(r'(\(\d\d\d\)) (\d\d\d-\d\d\d\d)')
>>> mo = phoneNumRegex.search('My phone number is (415) 555-4242.')
>>> mo.group(1)
'(415)'
>>> mo.group(2)
'555-4242'
2. 用管道匹配多個分組: "|" 第一次出現的匹配文本將作為Match對象返回。
>>> heroRegex = re.compile (r'Batman|Tina Fey') >>> mo1 = heroRegex.search('Batman and Tina Fey.') >>> mo1.group() 'Batman' >>> mo2 = heroRegex.search('Tina Fey and Batman.') >>> mo2.group() 'Tina Fey'
>>> batRegex = re.compile(r'Bat(man|mobile|copter|bat)')
>>> mo = batRegex.search('Batmobile lost a wheel')
>>> mo.group()
'Batmobile'
>>> mo.group(1)
'mobile
3. 用問號實現可選匹配: "?" 不論這段文本在不在, 正則表達式
都會認為匹配
>>> batRegex = re.compile(r'Bat(wo)?man') >>> mo1 = batRegex.search('The Adventures of Batman') >>> mo1.group() 'Batman' >>> mo2 = batRegex.search('The Adventures of Batwoman') >>> mo2.group() 'Batwoman'
>>> phoneRegex = re.compile(r'(\d\d\d-)?\d\d\d-\d\d\d\d')
>>> mo1 = phoneRegex.search('My number is 415-555-4242')
>>> mo1.group()
'415-555-4242'
>>> mo2 = phoneRegex.search('My number is 555-4242')
>>> mo2.group()
'555-4242
4. 用星號匹配零次或多次: "*"( 稱為星號)意味着“匹配零次或多次”,即星號之前的分組
>>> batRegex = re.compile(r'Bat(wo)*man') >>> mo1 = batRegex.search('The Adventures of Batman') >>> mo1.group() 'Batman' >>> mo2 = batRegex.search('The Adventures of Batwoman') >>> mo2.group() 'Batwoman' >>> mo3 = batRegex.search('The Adventures of Batwowowowoman') >>> mo3.group() 'Batwowowowoman
5.用加號匹配一次或多次: "+"則意味着“ 匹配一次或多次”
>>> batRegex = re.compile(r'Bat(wo)+man') >>> mo1 = batRegex.search('The Adventures of Batwoman') >>> mo1.group() 'Batwoman' >>> mo2 = batRegex.search('The Adventures of Batwowowowoman') >>> mo2.group() 'Batwowowowoman' >>> mo3 = batRegex.search('The Adventures of Batman') >>> mo3 == None True
6.用花括號匹配特定次數: "{}"如果想要一個分組重復特定次數,就在正則表達式中該分組的后面,跟上花括
號包圍的數字,除了一個數字,還可以指定一個范圍,即在花括號中寫下一個最小值、一個逗號和
一個最大值.
>>> haRegex = re.compile(r'(Ha){3}') >>> mo1 = haRegex.search('HaHaHa') >>> mo1.group() 'HaHaHa' >>> mo2 = haRegex.search('Ha') >>> mo2 == None True
貪心和非貪心匹配:
Python 的正則表達式默認是“ 貪心” 的,它們會盡
可能匹配最長的字符串,花括號的“ 非貪心” 版本匹配盡可能最短的字符串,即在
>>> greedyHaRegex = re.compile(r'(Ha){3,5}')
>>> mo1 = greedyHaRegex.search('HaHaHaHaHa')
>>> mo1.group()
'HaHaHaHaHa'
>>> nongreedyHaRegex = re.compile(r'(Ha){3,5}?')
>>> mo2 = nongreedyHaRegex.search('HaHaHaHaHa')
>>> mo2.group()
'HaHaHa'
findall() 和search() 方法:
search()方法: 返回一個 Match對象, 包含被查找字符串中的“ 第一次” 匹配的文本
>>> phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') >>> mo = phoneNumRegex.search('Cell: 415-555-9999 Work: 212-555-0000') >>> mo.group() '415-555-9999'
findall()方法:返回一組
字符串, 包含被查找字符串中的所有匹配findall()不是返回一個 Match 對象, 而是返回一個字符串列表,
只要在正則表達式中沒有分組。列表中的每個字符串都是一段被查找的文本, 它匹配該正則表達式。
>>> phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') # has no groups >>> phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000') ['415-555-9999', '212-555-0000']
如果在正則表達式中有分組, 那么 findall 將返回元組的列表
>>> phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)') # has groups >>> phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000') [('415', '555', '1122'), ('212', '555', '0000')]
字符分類
縮寫字符分類 表示
\d 0 到 9 的任何數字
\D 除 0 到 9 的數字以外的任何字符
\w 任何字母、數字或下划線字符(可以認為是匹配“單詞”字符)
\W 除字母、數字和下划線以外的任何字符
\s 空格、制表符或換行符(可以認為是匹配“空白”字符)
\S 除空格、制表符和換行符以外的任何字符
字符分類[0-5]只匹配數字 0 到 5
>>> xmasRegex = re.compile(r'\d+\s\w+') >>> xmasRegex.findall('12 drummers, 11 pipers, 10 lords, 9 ladies, 8 maids, 7 swans, 6 geese, 5 rings, 4 birds, 3 hens, 2 doves, 1 partridge') ['12 drummers', '11 pipers', '10 lords', '9 ladies', '8 maids', '7 swans', '6 geese', '5 rings', '4 birds', '3 hens', '2 doves', '1 partridge']
建立自己的字符分類:
1.字符分類[aeiouAEIOU]將匹配所有元音字符, 不論大小寫
>>> vowelRegex = re.compile(r'[aeiouAEIOU]') >>> vowelRegex.findall('RoboCop eats baby food. BABY FOOD.') ['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']
2.也可以使用短橫表示字母或數字范圍:字符分類[a-zA-Z0-9]將匹配所有小寫字母、 大寫字母和數字請注意,在方括號內,普通的正則表達式符號不會被解釋字符分類將匹配
數字 0 到 5 和一個句點。你不需要將它寫成[0-5.]
3.過在字符分類的左方括號后加上一個插入字符( ^), 就可以得到“ 非字符類”。
非字符類將匹配不在這個字符類中的所有字符。
>>> consonantRegex = re.compile(r'[^aeiouAEIOU]') >>> consonantRegex.findall('RoboCop eats baby food. BABY FOOD.') ['R', 'b', 'c', 'p', ' ', 't', 's', ' ', 'b', 'b', 'y', ' ', 'f', 'd', '.', ' ', 'B', 'B', 'Y', ' ', 'F', 'D', '.']
插入字符和美元字符:
1.可以在正則表達式的開始處使用插入符號( ^),表明匹配必須發生在被查找文本開始處。
2.可以再正則表達式的末尾加上美元符號( $),表示該字符串必須以這個正則表達式的模式結束。
>>> beginsWithHello = re.compile(r'^Hello') >>> beginsWithHello.search('Hello world!') <_sre.SRE_Match object; span=(0, 5), match='Hello'> >>> beginsWithHello.search('He said hello.') == None True >>> endsWithNumber = re.compile(r'\d$') >>> endsWithNumber.search('Your number is 42') <_sre.SRE_Match object; span=(16, 17), match='2'> >>> endsWithNumber.search('Your number is forty two.') == None True >>> wholeStringIsNum = re.compile(r'^\d+$') >>> wholeStringIsNum.search('1234567890') <_sre.SRE_Match object; span=(0, 10), match='1234567890'> >>> wholeStringIsNum.search('12345xyz67890') == None True >>> wholeStringIsNum.search('12 34567890') == None True
通配字符:
1.在正則表達式中,.(句點)字符稱為“通配符”,句點字符只匹配一個字符。
>>> atRegex = re.compile(r'.at') >>> atRegex.findall('The cat in the hat sat on the flat mat.') ['cat', 'hat', 'sat', 'lat', 'mat']
2.用點-星匹配所有字符( .*):
>>> nameRegex = re.compile(r'First Name: (.*) Last Name: (.*)') >>> mo = nameRegex.search('First Name: Al Last Name: Sweigart') >>> mo.group(1) 'Al' >>> mo.group(2) 'Sweigart'
3. 點-星使用“貪心” 模式:它總是匹配盡可能多的文本。要用“非貪心” 模式匹配所有文本, 就使用點-星和問號
>>> nongreedyRegex = re.compile(r'<.*?>') >>> mo = nongreedyRegex.search('<To serve man> for dinner.>') >>> mo.group() '<To serve man>' >>> greedyRegex = re.compile(r'<.*>') >>> mo = greedyRegex.search('<To serve man> for dinner.>') >>> mo.group() '<To serve man> for dinner.>'
4. 用句點字符匹配換行
點-星將匹配除換行外的所有字符。通過傳入 re.DOTALL 作為 re.compile()的第
二個參數, 可以讓句點字符匹配所有字符, 包括換行字符
>>> noNewlineRegex = re.compile('.*') >>> noNewlineRegex.search('Serve the public trust.\nProtect the innocent. \nUphold the law.').group() 'Serve the public trust.' >>> newlineRegex = re.compile('.*', re.DOTALL) >>> newlineRegex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group() nUphold the law.').group() 'Serve the public trust.\nProtect the innocent.\nUphold the law.'
正則表達式符號總結:
?匹配零次或一次前面的分組。
*匹配零次或多次前面的分組。
+匹配一次或多次前面的分組。
{n}匹配 n 次前面的分組。
{n,}匹配 n 次或更多前面的分組。
{,m}匹配零次到 m 次前面的分組。
{n,m}匹配至少 n 次、至多 m 次前面的分組。
{n,m}?或*?或+?對前面的分組進行非貪心匹配。
^spam 意味着字符串必須以 spam 開始。
spam$意味着字符串必須以 spam 結束。
.匹配所有字符,換行符除外。
\d、 \w 和\s 分別匹配數字、單詞和空格。
\D、 \W 和\S 分別匹配出數字、單詞和空格外的所有字符。
[abc]匹配方括號內的任意字符(諸如 a、 b 或 c)。
[^abc]匹配不在方括號內的任意字符。
不區分大小寫的匹配:
可以向 re.compile()傳入 re.IGNORECASE 或 re.I,作為第二個參數。
>>> robocop = re.compile(r'robocop', re.I) >>> robocop.search('RoboCop is part man, part machine, all cop.').group() 'RoboCop' >>> robocop.search('ROBOCOP protects the innocent.').group() 'ROBOCOP' >>> robocop.search('Al, why does your programming book talk about robocop so much?').group() 'robocop
用sub()方法替換字符串: