2011-11-14 47 views
3

所以我有一個模式:Python的簡單的regex

hourPattern = re.compile('\d{2}:\d{2}') 

而且對陣編譯模式

hourStart = hourPattern.match('Sat Jan 28 01:15:00 GMT 2012') 

當我打印hourStart它給了我無。任何幫助?

回答

9

匹配期望找到的值位於字符串的開頭。你想要搜索。從比賽方法

>>> import re 
>>> 
>>> s = re.compile('\d+') 
>>> 
>>> s2 = 'a123' 
>>> 
>>> s.match(s2) 
>>> s.search(s2) 
<_sre.SRE_Match object at 0x01E29AD8> 
+0

明白了,謝謝傢伙! – Jirico

+0

@Jirico - 樂於助人。請務必將答案標記爲已接受,以便其他用戶知道您已收到您所需的幫助。 :) –

+0

好的男人,我是一個新手,但我得到它! – Jirico

0

切換到搜索方法:

>>> hourPattern = re.compile('\d{2}:\d{2}') 
>>> hourStart = hourPattern.search('Sat Jan 28 01:15:00 GMT 2012') 
>>> hourStart.group() 
'01:15' 
+0

感謝您的幫助! – Jirico