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 |
acceptUnmatchedPattern(boolean acceptUnmatchedPattern)
Set if the collection accept the unmatched pattern.
|
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 |
isAcceptingUnmatchedPattern()
Return true if the collection accept the unmatched pattern.
|
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 acceptUnmatchedPattern(boolean acceptUnmatchedPattern)
RegexMatcher.UNMATCHED
instance will be returned by the matcher(java.lang.CharSequence)
method.
Note that by default the last pattern in the ordered list of patterns will be returned, regardless if it matches the input or not.acceptUnmatchedPattern
- true if the collection accept the unmatched patternpublic boolean isAcceptingUnmatchedPattern()
RegexMatcher.UNMATCHED
instance will be returned by the matcher(java.lang.CharSequence)
method.public 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
isAcceptingUnmatchedPattern()
returns true, then the behavior will be:
RegexMatcher
will be returnedRegexMatcher.UNMATCHED
matcherRegexCollection 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)