public class RegexCollection extends Object
CharSequence
against all the patterns. it simplifies the usage of several regex
expressions against the same input.Constructor and Description |
---|
RegexCollection() |
Modifier and Type | Method and Description |
---|---|
void |
addPattern(String name,
String regex)
Add a pattern for the collection of regex patterns.
|
void |
addPattern(String name,
String regex,
int flags)
Add a pattern for the collection of regex patterns.
|
void |
clear()
Clear the list of regex patterns.
|
RegexCollection |
deriveCollection()
Create a new RegexCollection from a previous one.
|
List<RegexPattern> |
getOrderedPatterns()
Return the ordered list of regex patterns.
|
Map<String,RegexPattern> |
getPatterns()
Return the Map of regex patterns.
|
boolean |
isEmpty()
Return true if the collection is empty.
|
RegexMatcher |
matcher(CharSequence input)
Return the appropriate matcher on a
CharSequence input. |
public RegexCollection deriveCollection()
public void addPattern(String name, String regex) throws PatternSyntaxException
name
- the pattern nameregex
- the pattern regexPatternSyntaxException
- If the expression's syntax is invalidpublic void addPattern(String name, String regex, int flags) throws PatternSyntaxException
name
- the pattern nameregex
- the pattern regexflags
- Match flagsPatternSyntaxException
- If the expression's syntax is invalidpublic void clear()
public boolean isEmpty()
public List<RegexPattern> getOrderedPatterns()
public Map<String,RegexPattern> getPatterns()
public RegexMatcher matcher(CharSequence input) throws EmptyRegexCollectionException
CharSequence
input.
RegexMatcher
will be returnedRegexMatcher
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 false
input
- the inputEmptyRegexCollectionException
- if the RegexCollection is empty (has no Patterns)