CharSequence
against all the patterns. It simplifies the usage of several regex expressions against the same input.
java.util.regex.Matcher
. The main methods of this class are:Matcher
class. For example:RegexMatcher matcher = collection.matcher("156"); String group = matcher.group(2);is equivalent to:
RegexMatcher matcher = collection.matcher("156"); String group = matcher.getMatcher().group(2);
RegexCollection collection = new RegexCollection(); collection.addPattern("number", "\\d+"); collection.addPattern("letters", "[A-Za-z]+"); RegexMatcher matcher = collection.matcher("156"); String name = matcher.getPatternName(); // return "number" boolean matches = matcher.matches(); // return true matcher = collection.matcher("Slayer"); name = matcher.getPatternName(); // return "letters" matches = matcher.matches(); // return true matcher = collection.matcher("1349 band"); name = matcher.getPatternName(); // return "letters" matches = matcher.matches(); // return falseThis second example shows how you can use the class with a switch case:
RegexCollection collection = new RegexCollection(); collection.addPattern("number", "\\d+"); collection.addPattern("letters", "[A-Za-z]+"); collection.addPattern("bother", "[A-Za-z]+ \\d+"); RegexMatcher matcher = collection.matcher("156"); if (matcher.matches()) { switch (matcher.getPatternName()) { case "number": System.out.println("This is a number"; break; case "letters": System.out.println("This is a word"; break; case "both": System.out.println("This is a word followed by a number"; break; } }
Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences