Python – Regex to Split 1st Colon

pythonregex

I have a time in ISO 8601 ( 2009-11-19T19:55:00 ) which is also paired with a name commence. I'm trying to parse this into two. I'm currently up to here:

import re
sColon = re.compile('[:]')

aString = sColon.split("commence:2009-11-19T19:55:00")

Obviously this returns:

>>> aString
['commence','2009-11-19T19','55','00']

What I'd like it to return is this:

>>>aString
['commence','2009-11-19T19:55:00']

How would I go about do this in the original creation of sColon? Also, do you recommend any Regular Expression links or books that you have found useful, as I can see myself needing it in the future!

EDIT:

To clarify… I'd need a regular expression that would just parse at the very first instance of :, is this possible? The text ( commence ) before the colon can chance, yes…

Best Answer

>>> first, colon, rest = "commence:2009-11-19T19:55:00".partition(':')

>>> print (first, colon, rest)
('commence', ':', '2009-11-19T19:55:00')