There is an undocumented Scanner class in the re module:
>>> from datetime import datetime
>>> from re import Scanner
>>> sc = Scanner([
... ("[0-9/]+ [0-9:]+", lambda self, s: datetime.strptime(s, "%m/%d/%Y %H:
%M:%S")),
... (r"\d+", lambda self, s: int(s)),
... ("\s+", lambda self, s: None)])
>>> sc.scan("07/24/2012 06:23:13")
([datetime.datetime(2012, 7, 24, 6, 23, 13)], '')
>>> sc.scan("07/24/2012 06:23:13 123")
([datetime.datetime(2012, 7, 24, 6, 23, 13), 123], '')
However:
>>> sc.scan("456 07/24/2012 06:23:13 123")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/re.py", line 322, in scan
action = action(self, m.group())
File "<stdin>", line 2, in <lambda>
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '456 07' does not match format '%m/%d/%Y %H:%M:%S'