import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?<title>.+?)[_.\\s-]+(?<season>S\\d+)(?:[.\\-\\s]*?(?<seasonmax>S?\\d+))?(?=[_.\\s](?!E\\d+))";
final String string = "Cops.S35E18.1080p.WEB.h264-BAE.mkv\n"
+ "Anger Management (2012) - S02E15 - Charlies Patients Hook Up [WEBDL-1080p][AC3 5.1][h264]-BS.mkv\n"
+ "Ahsoka (2023) - S01E01 - Part One Master and Apprentice [DSNP WEBDL-1080p][EAC3 Atmos 5.1][h264]-NTb.mkv'\n"
+ "Loki....... - S01E01 - Glorious Purpose [DSNP WEBDL-1080p Proper][EAC3 Atmos 5.1][h264]-TOMMY.mkv'\n"
+ "Family - Guy (1999) - S01E01 - Death Has a Shadow [DSNP WEBDL-1080p][AAC 2.0][h264]-PHOENiX.mkv'\n"
+ "Neighbours.S39E012.2023-10-05.Episode.8915.1080p.AMZN.WEB-DL.DDP2.0.H.264-SDCC\n"
+ "Rick and Morty (2013) - S06E06 - JuRicksic Mort [HMAX DD 5.1 WEBDL-1080p][x264]-NTb.mkv\n\n"
+ "Hello.World.S01.ntb.mkv\n"
+ "Hello.World.S01 - S03.ntb.mkv\n"
+ "Hello.World.S1.ntb.mkv\n"
+ "Hello.World.S01 S03.ntb.mkv\n"
+ "Hello.World.S01.Extras.ntb.mkv\n"
+ "Hello_World_S01_S02.ntb.mkv\n"
+ "Hello_World_S01-S03_ntb.mkv\n"
+ "Hello_World_S1_ntb.mkv\n\n"
+ "Hello.World.S1E03.ntb.mkv\n"
+ "Hello.World.S031 E01.ntb.mkv\n"
+ "Hello_World_S01E01_ntb.mkv\n"
+ "Hello_World_S1E03_ntb.mkv\n"
+ "Hello.World.S01.E01.ntb.mkv\n"
+ "Hello_World_S01_E01_ntb.mkv";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html