3

So I have a pattern:

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

And match against the compiled pattern

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

When I print hourStart it gives me None. Any help?

0

2 Answers 2

9

Match expects the found value to be at the beginning of the string. You want search.

>>> import re
>>>
>>> s = re.compile('\d+')
>>>
>>> s2 = 'a123'
>>>
>>> s.match(s2)
>>> s.search(s2)
<_sre.SRE_Match object at 0x01E29AD8>
Sign up to request clarification or add additional context in comments.

1 Comment

@Jirico - Happy to help. Be sure to mark answers as accepted so that other users know you've received the help you need. :)
0

Switch from the match method to the search method:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.