From 124d65271bb6db7b9065b6567b7d806f266a7384 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 19 Sep 2013 11:45:41 +0200 Subject: [PATCH] Use multiple patterns instead of one large pattern. --- .../rhynodge/filters/EpisodeFilter.java | 27 +++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/EpisodeFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/EpisodeFilter.java index 7709d9e..f206dc3 100644 --- a/src/main/java/net/pterodactylus/rhynodge/filters/EpisodeFilter.java +++ b/src/main/java/net/pterodactylus/rhynodge/filters/EpisodeFilter.java @@ -18,9 +18,11 @@ package net.pterodactylus.rhynodge.filters; import static com.google.common.base.Preconditions.checkState; +import static java.util.Arrays.asList; import java.util.HashMap; import java.util.Map; +import java.util.Collection; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -41,7 +43,7 @@ import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; public class EpisodeFilter implements Filter { /** The pattern to parse episode information from the filename. */ - private static Pattern episodePattern = Pattern.compile("S(\\d{2})E(\\d{2})|[^\\d](\\d{1,2})x(\\d{2})[^\\d]"); + private static final Collection episodePatterns = asList(Pattern.compile("S(\\d{2})E(\\d{2})"), Pattern.compile("[^\\d](\\d{1,2})x(\\d{2})[^\\d]")); // // FILTER METHODS @@ -87,19 +89,18 @@ public class EpisodeFilter implements Filter { * information could be found */ private static Episode extractEpisode(TorrentFile torrentFile) { - Matcher matcher = episodePattern.matcher(torrentFile.name()); - if (!matcher.find()) { - return null; - } - String seasonString = matcher.group(1); - String episodeString = matcher.group(2); - if ((seasonString == null) && (episodeString == null)) { - seasonString = matcher.group(3); - episodeString = matcher.group(4); + for (Pattern episodePattern : episodePatterns) { + Matcher matcher = episodePattern.matcher(torrentFile.name()); + if (!matcher.find() || matcher.groupCount() < 2) { + continue; + } + String seasonString = matcher.group(1); + String episodeString = matcher.group(2); + int season = Integer.valueOf(seasonString); + int episode = Integer.valueOf(episodeString); + return new Episode(season, episode); } - int season = Integer.valueOf(seasonString); - int episode = Integer.valueOf(episodeString); - return new Episode(season, episode); + return null; } } -- 2.7.4