1

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);
    }
}

}

user3566896
  • 97
  • 3
  • 9
  • @Luiggi Mendoza, I should have specified though that there are other strings that include other values than the three mentioned, so I can't look specifically for those values. – user3566896 Jun 13 '14 at 14:22
  • *All I want to be able to collect from these strings is STWR, STW, or ST* you should edit your question and post what you really want/need. – Luiggi Mendoza Jun 13 '14 at 14:24
  • 1
    You should give examples on all possible patterns the regex should match, otherwise you would not get helpful answers. – Farhad Alizadeh Noori Jun 13 '14 at 14:24
  • You both are right, I should have been more specific. I edited my question, let me know if it is still misleading. – user3566896 Jun 13 '14 at 14:31
  • 1
    `== "II"` no, No, NO. Read [how-do-i-compare-strings-in-java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Pshemo Jun 13 '14 at 14:33
  • @Pshemo, thanks I'm still a greenhorn, so I'll definitely look that over. – user3566896 Jun 13 '14 at 14:40

2 Answers2

2

You can use this regex using negative lookahead:

"\\b((?!I+\\b)[A-Z]{2,4})\\b"

Working Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks for the help, you're answer works great. Would you mind explaining to me what \\b and ?! specifically do? – user3566896 Jun 13 '14 at 14:33
  • `\b` is for word boundary and `(?!..)` is for negative lookahead. That makes sure that `(?!I+\\b)` will fail when there are only one or more `I` letters. – anubhava Jun 13 '14 at 14:36
1

Use the following statement to get the upper-case letters at the end:

String caps = str.replaceAll(".*?([A-Z]+)[^A-Z]*$", "$1");

The regex captures the target, which is then returned as a backreference.


Also - Bug alert!

if (answer2 == "II" | answer2 == "III") {

You can't use == to compare Strings in Java! Use .equals() instead:

if (answer2.equals("II") || answer2.equals("III")) {
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Bohemian
  • 412,405
  • 93
  • 575
  • 722