2012-05-26 53 views
2

爲什麼這個正則表達式不工作?正則表達式之間的一切,Python

import re 
i="<wx._controls.Button; proxy of <Swig Object of type 'wxButton *' at 0x2d040b0> >" 
m = re.match("controls(.*)[;]", i) 
if m: 
    print m.group(1) 

它什麼都沒有返回。我試圖讓所有事情都處於中間,「控制」和「;」 該解決方案適用於其他測試用例,但不適用於此測試用例。

+5

閱讀本文[Python的re.search和re.match有什麼區別?](http://stackoverflow.com/questions/180986/what-is-the-difference-between-pythons-re-search-和重賽) – RanRag

回答

5

re.match只匹配字符串的開頭。你想要re.search

但是,它看起來像你正在評估對象的結果repr以獲取類名稱。爲什麼不使用obj.__class__.__name__代替?使用duck typing並避免特定於各個類的代碼。

4

.match()從頭開始。你想要.search()