I am having an issue understanding regular expressions. All I want to be able to collect are the capital letters toward the end of the strings. The strings listed are just an example of the numerous strings I have, so searching specifically for STWR, STW, or ST won't work. The way my regex works I keep getting II or III as a result. Is there a way to gather the information I want but exclude II or III? Will I be able to do it within my regex pattern, or do I need an if loop? Here is my code along with an if loop I have tried. Any help would be much appreciated.
public class First {
public static void main(String[] args) {
String one = "Star Wars I - 00001 - STWR ep1";
String two = "Star Wars II - 00002 - STW ep2";
String three = "Star Wars III - 00003 - ST ep3";
Pattern p = Pattern.compile("([A-Z]{2,4})|[A-Z]{2}");
Matcher m = p.matcher(one);
m.find();
String answer1 = m.group();
System.out.println("This is answer 1: " + answer1);
Matcher n = p.matcher(two);
n.find();
String answer2 = n.group();
System.out.println("This is answer 2: " + answer2);
Matcher o = p.matcher(three);
o.find();
String answer3 = o.group();
System.out.println("This is answer 3: " + answer3);
if (answer2 == "II" | answer2 == "III") {
p = Pattern.compile("([A-Z]{3,4})");
System.out.println(answer2);
}
}
}