import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?!Vol)(?!Chapter)(?<Series>.+?)(-|_|\\s|#)\\d+(-\\d+\\b)?(.+?(?=\\s+->))?";
final String string = "Test cases:\n"
+ "Kodoja #001 (March 2016) -> Kodoja\n"
+ "Bleach 001-002 -> Bleach\n"
+ "[BAA]_Darker_than_Black_Omake-1 -> [BAA]_Darker_than_Black_Omake\n\n"
+ "Edge cases:\n"
+ "The Archmage Returns After 4000 Years -> The Archmage Returns After\n"
+ "See You in My 19th Life -> See You in My\n"
+ "The Return of the 8th Class Mage -> The Return of the\n"
+ "Kaiju No. 8 -> Kaiju No.\n"
+ "Zom 100 - Bucket List of the Dead -> Zom";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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