1

I'm trying to make a program that organize my TV Show collection.

My regex need to recognize the episode number given the file name of the episode. Please note that sometimes a single video file contains more episodes.

In my particular case I only need to parse episodes named like:

"s01e01.avi" expected result= 1

"s01e01 & s01e02.avi" expected result= 1, 2

"s01e01.02.avi" expected result= 1, 2

"s01e03.04 s01e05.06.avi" expected result= 3,4,5,6

"s01e03.04.05.06.avi" expected result= 3,4,5,6

final Matcher m = Pattern.compile("s[0-9]{1,2}e([0-9]{1,2})(\\.[0-9]{1,2})*").matcher(fileName);
while(m.find()) {
    for (int i = 1; i < m.groupCount(); i++) {
        System.out.println("myEpisodeNumer = " + m.group(i));
    }
}

That's what I've got, but it works only in the case where there's a single episode per group:

WORKS

"s01e01.avi" result: 1, correct

"s01e01 & s01e02.avi" result: 1, 2, correct

DON'T WORK

"s01e01.02.avi" (Only matches the first occurrence, 1 in this case) expected result: 1,2

"s01e01.02 s01e03.04.avi" (Only matches the first occurrence for each group, 1 and 3 in this case)expected result: 1,2,3,4

Thanks a lot for your time

3 Answers 3

3

try this: (didn't adjust to fit java syntax)

(?<=s\d\de)(\d\d)|(?<=\d\d[.])(\d\d)*

example here: see the captured groups:

http://regex101.com/r/jH3yZ5/4

Sign up to request clarification or add additional context in comments.

2 Comments

You are basically using the same regex as mine... And I don't know why, but it don't work... try something like s01e01.02.03
@Spotlight ok, you didn't put that in example, I would think about it now, so you have sxxexx.xx.xx.xx.xx ...(n) parts but I don't think this deserve a downvote.. :-(
2

Why not just

e(\d\d)\.?(\d\d)?

As long as you don't have any TV shows named "The55 show" or similar, it's not going to break. The episode numbers will be captured in the groups of the match.

See it in action here.

3 Comments

the thing is, OP has s01e03.04 read the question twice pls.
@Kent Fixed. There's still no need to match the s... portion.
The problem is that if you "legally acquire" a Episode, the name is really messed up, so I want a regex more accurrate as possible: many times i see something like "s01e03 by P0se1don.avi" (the e1 inside Poseidon is gonna break the regex)
1

The problem is that a single regex can't easily return a list of values. (Even if you put * or + after a capture group, this does not cause more capture groups to be added; the number of capture groups is a fixed value based on the pattern only, not on the source string.)

So while you did well to write a loop to find each portion beginning with s, you still have the problem that the portions beginning with s may themselves contain lists of episode numbers. While you may be able to find a tricky way to do this with a single loop, I'd recommend using a nested find loop, or split. Your first pattern should look like this:

final Matcher m = Pattern.compile("s[0-9]{1,2}e([0-9]{1,2}(?:\\.[0-9]{1,2})*)").matcher(fileName);

Note that the part that matches multiple occurrences of things like .02, .03, etc., has been moved inside the first capture group. (I put ?: in the second set of parentheses to emphasize the fact that this is not a capture group that you will be extracting with group().) After you do that, the result of group(1) could be "01" or "01.02" or "03.04.05" or whatever. Now you can split on the period character:

while (m.find()) {
    for (String episode : m.group(1).split("\\.")) {
        System.out.println("myEpisode = " + episode);
    }
}

P.S. This is an approach that will work generally on similar kinds of problems. Your particular problem is simple enough to solve with a single loop (because the combination .\d\d doesn't occur anywhere else except inside one of the s\d\de....... patterns), but there will be situations where that approach won't work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.