Is there a way to match a pattern (e\d\d) several times, capturing each one into a group? For example, given the string..
blah.s01e24e25
..I wish to get four groups:
1 -> blah
2 -> 01
3 -> 24
4 -> 25
The obvious regex to use is (in Python regex:
import re
re.match("(\w+).s(\d+)e(\d+)e(\d+)", "blah.s01e24e25").groups()
..but I also want to match either of the following:
blah.s01e24
blah.s01e24e25e26
You can’t seem to do (e\d\d)+, or rather you can, but it only captures the last occurrence:
>>> re.match("(\w+).s(\d+)(e\d\d){2}", "blah.s01e24e25e26").groups()
('blah', '01', 'e25')
>>> re.match("(\w+).s(\d+)(e\d\d){3}", "blah.s01e24e25e26").groups()
('blah', '01', 'e26')
I want to do this in a single regex because I have multiple patterns to match TV episode filenames, and do not want to duplicate each expression to handle multiple episodes:
\w+\.s(\d+)\.e(\d+) # matches blah.s01e01
\w+\.s(\d+)\.e(\d+)\.e(\d+) # matches blah.s01e01e02
\w+\.s(\d+)\.e(\d+)\.e(\d+)\.e(\d+) # matches blah.s01e01e02e03
\w - \d+x\d+ # matches blah - 01x01
\w - \d+x\d+\d+ # matches blah - 01x01x02
\w - \d+x\d+\d+\d+ # matches blah - 01x01x02x03
..and so on for numerous other patterns.
Another thing to complicate matters – I wish to store these regexs in a config file, so a solution using multiple regexs and function calls is not desired – but if this proves impossible I’ll just allow the user to add simple regexs
Basically, is there a way to capture a repeating pattern using regex?
After thinking about the problem, I think I have a simpler solution, using named groups.
The simplest regex a user (or I) could use is:
The filename parsing class will take the first group as the show name, second as season number, third as episode number. This covers a majority of files.
I’ll allow a few different named groups for these:
To support multiple episodes, I’ll support two named groups, something like
startingepisodenumberandendingepisodenumberto support things likeshowname.s01e01-03:And finally, allow named groups with names matching
episodenumber\d+(episodenumber1,episodenumber2etc):It still requires possibly duplicating the patterns for different amounts of
e01s, but there will never be a file with two non-consecutive episodes (likeshow.s01e01e03e04), so using thestarting/endingepisodenumbergroups should solve this, and for weird cases users come across, they can use theepisodenumber\d+group namesThis doesn’t really answer the sequence-of-patterns question, but it solves the problem that led me to ask it! (I’ll still accept another answer that shows how to match
s01e23e24...e27in one regex – if someone works this out!)