Hi, I need some help with ReGex, I'm trying to separate timestamps from title tracks. Timestamps can be to the left or right of the title
1. Master Morality 00:00:03
...
39:17 9. Pechmarie
TIMESTAMP_PATTERN = r"((?:\d{2}:)+(?:\d{2}){1})"
with open(txt, "r", encoding="utf-8") as f:
for song in f.readlines():
song = "".join([c for c in song if c.isprintable()])
song_data = re.split(TIMESTAMP_PATTERN, song.strip())
print(song_data)
# output
['1. Master Morality ', '00:00:03', '']
...
['', '39:17', ' 9. Pechmarie']
I have a few issues with this:
- I have empty strings on the split
- I need to use
song = "".join([c for c in song if c.isprintable()])to remove newline and other control characters from matches (is there a better way of doing this?) - I need to get the name of the song and the timestamp separately. Right now using split, the name of the song can either be the first or the last element, which is not convenient
-> Is there any way of getting two groups, one that matches theTIMESTAMP_PATTERNand another that matches all the rest?
Thanks!