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

}

6
  • @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. Commented Jun 13, 2014 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. Commented Jun 13, 2014 at 14:24
  • 1
    You should give examples on all possible patterns the regex should match, otherwise you would not get helpful answers. Commented Jun 13, 2014 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. Commented Jun 13, 2014 at 14:31
  • 1
    == "II" no, No, NO. Read how-do-i-compare-strings-in-java. Commented Jun 13, 2014 at 14:33

2 Answers 2

2

You can use this regex using negative lookahead:

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

Working Demo

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help, you're answer works great. Would you mind explaining to me what \\b and ?! specifically do?
\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.
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")) {

3 Comments

I'm not sure how this answers the question?
Thanks for the info that will be helpful later. That explains why I've been going in circles. Looks like I need to reread a chapter in the book I'm going through.
@AmalMurali there. A one line solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.