1. 程式人生 > >Python3 正則表示式中group()方法獲得匹配結果

Python3 正則表示式中group()方法獲得匹配結果

正則表示式中用match()方法可以獲得匹配的字串內容。

如果想從字串中提取出一部分內容,可以用括號將提取目標括起來。

括號()實際上標記了一個子表示式的開始和結束的位置,被標記的每個子表示式會依次對應每個分組,呼叫group()方法傳入分組的索引即可獲得提取的結果。

注意:group()方法的分組索引從1開始;預設索引為0,表示匹配到的結果。

          groups()方法則是所有分組(索引從1開始)組成的元組。

匹配物件方法 描述
group(num=0) 匹配的整個表示式的字串,group() 可以一次輸入多個組號,在這種情況下它將返回一個包含那些組所對應值的元組。
groups() 返回一個包含所有小組字串的元組,從 1 到 所含的小組號。

例1

import re


content = 'Hello 123456789 Word_This is just a test 666 Test'
result = re.match('^Hello\s(\d+).*?Test', content)  # 注意(\d+) 有括號,+號表示匹配一次或多次

print(result)
print(result.group())    # print(result.group(0)) 同樣效果
print(result.groups())

print(result.span())     
print(result.group(1))

結果:

<_sre.SRE_Match object; span=(0, 49), match='Hello 123456789 Word_This is just a test 666 Test>
Hello 123456789 Word_This is just a test 666 Test
('123456789',)
(0, 49)
123456789

Process finished with exit code 0

可以看到group()是匹配到的結果,group(1)就是 (\d+) 匹配到的數字 123456789,groups()是 只有group(1)的元組。

如果正則表示式中還有(),則結果還會有group(2)等,groups()中就是group(1)、group(2)、...group(n)等組成的元組。

例2

import re


content = 'Hello 123456789 Word_This is just a test 666 Test'
result = re.match('^Hello\s(\d+).*?(\d+)\sTest', content)  # 注意第2個(\d+)前面是非貪婪模式

print(result)
print(result.group())    # print(result.group(0)) 同樣效果
print(result.groups())

print(result.span())
print(result.group(1))
print(result.group(2))

  結果:

<_sre.SRE_Match object; span=(0, 49), match='Hello 123456789 Word_This is just a test 666 Test>
Hello 123456789 Word_This is just a test 666 Test
('123456789', '666')
(0, 49)
123456789
666

Process finished with exit code 0

可以看到第2個 (\d+) 匹配到的是 666,也就是group(2)中的內容,groups()中是有group(1)和group(2)的元組。

例3:貪婪模式下的匹配,將例2中的 .*? 改為 .*

import re


content = 'Hello 123456789 Word_This is just a test 666 Test'
result = re.match('^Hello\s(\d+).*(\d+)\sTest', content)  # 注意第2個(\d+)前面是貪婪模式

print(result)
print(result.group())    # print(result.group(0)) 同樣效果
print(result.groups())

print(result.span())
print(result.group(1))
print(result.group(2))

 結果:

<_sre.SRE_Match object; span=(0, 49), match='Hello 123456789 Word_This is just a test 666 Test>
Hello 123456789 Word_This is just a test 666 Test
('123456789', '6')
(0, 49)
123456789
6

Process finished with exit code 0

 可以看到貪婪模式下 group(2)中的 666 變為了6,前面的2個6被“貪婪”了,僅匹配 (\d+)中的最低要求,即匹配一個數字。

 在做匹配時,字串中間儘量使用非貪婪模式。

例4:非貪婪模式.*? 的位置

(1)在字串末尾就有可能匹配不到任何內容:

import re


content = 'Hello 123456789 Word_This is just a test 666 Test'
result = re.match('^Hello\s(\d+).*?(\d+).*?', content)  # 注意正則表示式末尾處的非貪婪模式.*?

print(result)
print(result.group())

 結果:

<_sre.SRE_Match object; span=(0, 44), match='Hello 123456789 Word_This is just a test 666'>
Hello 123456789 Word_This is just a test 666

Process finished with exit code 0

 (2) 當貪婪模式.*在末尾時:

import re


content = 'Hello 123456789 Word_This is just a test 666 Test'
result = re.match('^Hello\s(\d+).*?(\d+).*', content)  # 注意正則表示式末尾處的貪婪模式.*

print(result)
print(result.group()) 

 結果:

<_sre.SRE_Match object; span=(0, 49), match='Hello 123456789 Word_This is just a test 666 Test>
Hello 123456789 Word_This is just a test 666 Test

Process finished with exit code 0

  通過對比,可以發現  貪婪模式在正則表示式的末尾時匹配到了666後面的內容,而非貪婪模式則沒有匹配666後面的內容。

參考:

《Python3網路爬蟲開發實戰》,崔慶才 著,3.3,p139-145.