2008-12-10 39 views
6

我使用re.findall()來提取一個HTML文件的一些版本號:Python的正則表達式的findall數字和點

>>> import re 
>>> text = "<table><td><a href=\"url\">Test0.2.1.zip</a></td><td>Test0.2.1</td></table> Test0.2.1" 
>>> re.findall("Test([\.0-9]*)", text) 
['0.2.1.', '0.2.1', '0.2.1'] 

,但我想只能得到不以終止那些點。 文件名可能並不總是.zip,所以我不能將.zip粘貼到正則表達式中。

我想結束:

['0.2.1', '0.2.1'] 

任何人都可以提出一個更好的正則表達式來使用? :)

回答

12
re.findall(r"Test([0-9.]*[0-9]+)", text) 

,或者短一點:

re.findall(r"Test([\d.]*\d+)", text) 

順便說一句 - 你不能逃脫點在字符類:

[\.0-9] // matches: 0 1 2 3 4 5 6 7 8 9 . \ 
[.0-9] // matches: 0 1 2 3 4 5 6 7 8 9 . 
+0

偉大的作品,非常感謝! – Ashy 2008-12-10 15:42:25