Problem : I have following sample strings:
ex1 = "00:03:34 hello!! this is example number 1 00:04:00"
ex2 = "00:07:08 Hi I am example number 2"
I want it grouped like below (output) :
ex1 out : ("00:03:34", "hello!! this is example number 1", "00:04:00")
ex2 out : ("00:07:08", "Hi I am example number 2", None)
Tries :
I ve tried re split :
time_pat = r"(\d{2}:\d{2}:\d{2})"
re.split(time_pat, ex1)
re.split(time_pat, ex2)
it gives me following output:
ex1 out : ['', '00:03:34', ' hello!! this is example number 1 ', '00:04:00', '']
ex2 out : ['', '00:07:08', ' Hi I am example number 2']
I will get rid of blanks using filter and the output will then look like
ex1 out : ['00:03:34', ' hello!! this is example number 1 ', '00:04:00']
ex2 out : ['00:07:08', ' Hi I am example number 2']
The problem here is ex2 output will be of length 2 not 3, with the 3rd elemet as None. I know if the length is of 2, I can append None But I dont want to do that and I believe regular expression can do that.
I ve tried the following regular expressions:
re1 : r"(\d{2}:\d{2}:\d{2})(.*)(\d{2}:\d{2}:\d{2})"
as quite obvious, it will parse ex1 but not ex2
re2 : r"(\d{2}:\d{2}:\d{2})(.*)(\d{2}:\d{2}:\d{2})?"
this will parse both but 3rd string is always None since ".*" in regular expression consumes the end time pattern.
I ve tried lookahead assertion but I mite have tried it wrong thus giving no result. Can anybody help me get the regular expression here?
Hi I am example number 2?