Created
June 19, 2015 07:06
-
-
Save atextor/cca2cdb92092dd94c48c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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