Season: 01 - Episodes: 01-02-03
The simple code at the bottom outputs the string above (as seen at the bottom of the Java demo). But you said you'd like some explanations, so we'll proceed step by step.
Step-By-Step
Let's first build a simple regex. Then we'll refine the output for your needs.
Search: ^.*?s(\d{2})((?:e\d{2})+)\..*
Replace: Season: $1 - Episodes: $2
Output: Season: 01 - Episodes: e01e02e03
In the regex101 demo, see the substitutions at the bottom. In the Java code below, we won't replace anything. This is just to see how things work.
Explaining the Match
^
asserts that we are at the beginning of the string
.*?
lazily matches characters, up to...
s(\d{2})
matches s
, then the parentheses capture two digits to Group 1
- The outer parentheses in
((?:e\d{2})+)
define capture Group 2
- The non-capturing group
(?:e\d{2})
matches e
and two digits, and
- The
+
quantifier ensures we do that once or more, allowing us to capture all the episodes in to Group 2
\.
matches the period before the extension
.*
matches the end of the string
Explaining the Replacement
- In the code below, we won't have the
e
between the episodes.
Season:
writes the literal characters Season:
$1
is a back-reference to Group 1, and inserts the season
- Episodes:
inserts the literal characters - Episodes:
$2
is a back-reference to Group 2, and inserts the episodes
Going Further: Dashes between Episode Numbers (or other refinements)
Let's say you want Season: 01 - Episodes: 01-02-03
This is not possible in a simple regex search and replace in a text editor, but it is easy in a programming language that allows you to use the capture groups of your match to build an output string.
Here is sample Java code (see the output at the bottom of the online demo):
String subject = "showname.s01e01e02e03.extension";
Pattern regex = Pattern.compile("^.*?s(\\d{2})((?:e\\d{2})+).*");
Matcher m = regex.matcher(subject);
String myoutput = "No Match"; // initialize
if (m.find()) {
myoutput = "Season: " + m.group(1) +" - Episodes: " ;
myoutput += m.group(2).substring(1,m.group(2).length()).replace("e", "-");
}
System.out.println(myoutput);
How the Code Works
- We use our regex from above
- For our match, we build an output string in several steps
- As in the simple demo,
myoutput = "Season: " + m.group(1) +" - Episodes: " ;
gives us some literal characters, Group 1 (the season), and more literal characters
- For the episodes string, instead of using Group 2 directly (i.e.
m.group(2)
), we replace all the e
characters with dashes: replace("e", "-")
... But only starting after the first character, as we don't want to replace the first e
with a dash: m.group(2).substring(1,m.group(2).length())