I have a set of strings in the format of "Document HH:MM:SS DD-MM-YY" where the upper case letters denote numbers.
I want to be able to process them into something like
[hh,mm,ss,dd,mm,yy] = string.process("Document XX:XX:XX XX-XX-XX")
so basically I tell Python that anything X in string should be copied to one of the outputs, and each contiguous sequence of X's is a single output in the left handside array.
Is that possible in an easy way in Python?
Since everything is in fixed indices, I could use substrings with fixed indices, but would rather not.
CodePudding user response:
We can use re.findall
here:
inp = "Document 12:34:56 04-08-22"
matches = re.findall(r'\b(\d{2}):(\d{2}):(\d{2}) (\d{2})-(\d{2})-(\d{2})\b', inp)
print(matches) # [('12', '34', '56', '04', '08', '22')]
For input strings with more than one timestamp match, the output list would have multiple tuples.