Skip to content

Instantly share code, notes, and snippets.

@atextor
Created June 19, 2015 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atextor/cca2cdb92092dd94c48c to your computer and use it in GitHub Desktop.
Save atextor/cca2cdb92092dd94c48c to your computer and use it in GitHub Desktop.
package regex;
import java.util.regex.Pattern;
public class RegexSwitch {
enum RegexCase {
HELLO("Hello.*"),
FOOD("x?Food$"),
NOMATCH(null);
private Pattern pattern;
RegexCase(String regex) {
this.pattern = regex == null ? null : Pattern.compile(regex);
}
public static RegexCase valueOfString(String s) {
for (RegexCase r : RegexCase.values()) {
if (r.pattern != null && r.pattern.matcher(s).matches()) {
return r;
}
}
return NOMATCH;
}
}
public static void main(String[] args) {
String input = "Hello World";
// String input = "xFood";
switch(RegexCase.valueOfString(input)) {
case HELLO: System.out.println("Eingabe war Hello World"); break;
case FOOD: System.out.println("Eingabe endet mit Food"); break;
default: System.out.println("Unbekannte Eingabe");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment