2015-11-23 44 views
-2

我有以下Python代碼:如何理解python中的re.match?

import re 

result = re.match('a.*b', 'aabab') 
result.groups() # result is() 
len(result.groups()) # it's 0 

result.group(0) # result is 'aabab' 

我只知道一些基本的regex,但我不明白的groupsgroup。有人可以對此進行一些基本的解釋。

以上,如果可能的話,請在python中給出關於PatternMatcher的一些說明。

+2

你讀過[文檔](https://docs.python.org/2/library/regex.html)嗎? – MattDMo

+1

特別指出,re庫[[Match Object]]上的文檔(https://docs.python.org/2/library/re.html#match-objects) –

+0

請看我的答案:http: //stackoverflow.com/a/21249114/3182836 – binarysubstrate

回答

0

組用於捕獲正則表達式的特定部分。您的正則表達式不定義任何組,因此組計數爲零。

組(0)是一個特殊情況,它包含整個匹配。