25
void menu() {
    print();
    Scanner input = new Scanner( System.in );
    while(true) {
        String s = input.next();
        switch (s) {
        case "m": print(); continue;
        case "s": stat(); break;
        case "[A-Z]{1}[a-z]{2}\\d{1,}": filminfo( s ); break;
        case "Jur1": filminfo(s); break; //For debugging - this worked fine
        case "q": ; return;
        }
    }
}

It seems like either my regex is off or that I am not using it right in the case-statement. What I want is a string that: Begins with exactly one uppercase letter and is followed by exactly two lowercase letters, which are followed by at least one digit.

I've checked out the regex API and tried the three variants (greedy, reluctant and possessive quantifiers) without knowing their proper use. Also checked the methods for String without finding a method that seemed pertinent to my needs.

4
  • 2
    Is this something new in Java 7? switch with regex. There is no such a thing in Java 6 or under. Commented Nov 11, 2011 at 0:44
  • 1
    there is even no string switch in 6 and below (only integrals and enums) Commented Nov 11, 2011 at 0:46
  • Yes - I suspected that this was breaking the rules of switch. So over to good 'ol if/else if-statements then? Commented Nov 11, 2011 at 0:47
  • Man, I love this website! Thanks. Commented Nov 11, 2011 at 0:49

3 Answers 3

45

You can't use a regex as a switch case. (Think about it: how would Java know whether you wanted to match the string "[A-Z]{1}[a-z]{2}\\d{1,}" or the regex?)

What you could do, in this case, is try to match the regex in your default case.

    switch (s) {
        case "m": print(); continue;
        case "s": stat(); break;
        case "q": return;
        default:
            if (s.matches("[A-Z]{1}[a-z]{2}\\d{1,}")) {
                filminfo( s );
            }
            break;
    }

(BTW, this will only work with Java 7 and later. There's no switching on strings prior to that.)

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

3 Comments

Awesome way to handle the case :)
You can do that in JS, though
@SergeyZolotarev: Closest thing you can do in JS is a switch (true) block testing each regex...which is close, but not this, cause now you can't treat the strings as cases anymore (you have to say like case (s === 'm'): rather than case 'm':).
6

I don't think you can use regex in switch cases.

The String in the switch expression is compared with the expressions associated with each case label as if the String.equals method were being used.

See http://download.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html for more info.

1 Comment

Thank you for deepening my understanding of switch-statements.
2

Finally after over 10 years, in Java 21; you should be able to do this with pattern matching for switches:

https://openjdk.org/jeps/441

The example code should reduce to:

void menu() {
    print();
    Scanner input = new Scanner( System.in );
    var pattern = Pattern.compile("[A-Z]{1}[a-z]{2}\\d{1,}");
    while(true) {
        String inputString = input.next();
        switch (inputString) {
           case "m" -> {
              print();
              stat();
           }
           case "s" -> stat();
           case "Jur1" -> filminfo(inputString); //For debugging - this worked fine
           case "q" -> return;
           case String s when pattern.matcher(s).matches() -> filminfo(s);
           default -> {}
        }
    }
}

Comments

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.