Using regex for switch-statement in Java
Asked Answered
M

3

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.

Maryjanemaryjo answered 11/11, 2011 at 0:41 Comment(4)
Is this something new in Java 7? switch with regex. There is no such a thing in Java 6 or under.Bloomer
there is even no string switch in 6 and below (only integrals and enums)Ingratiating
Yes - I suspected that this was breaking the rules of switch. So over to good 'ol if/else if-statements then?Maryjanemaryjo
Man, I love this website! Thanks.Maryjanemaryjo
B
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.)

Butta answered 11/11, 2011 at 0:49 Comment(3)
Awesome way to handle the case :)Forsberg
You can do that in JS, thoughPersonate
@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':).Butta
T
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.

Turro answered 11/11, 2011 at 0:55 Comment(1)
Thank you for deepening my understanding of switch-statements.Maryjanemaryjo
M
1

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 -> {}
        }
    }
}
Micaelamicah answered 20/9, 2023 at 21:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.