Class TimeLimitedMatcher
Instances of this class, created through one of its two factory methods, will either succeed
or timeout, throwing a TimeLimitedMatcher.TimeoutException. It is incumbent on the caller to handle the
exception.
N.B. This matcher differs from the standard Matcher in that the factory method
executes the matches() check directly, failing and throwing an exception if it times out.
Subsequent calls to the matches() method merely return the result of the initial check;
they do not re-execute the search.
Example usage:
try {
Pattern pattern = Pattern.compile("bc");
TimeLimitedMatcher matcher = TimeLimitedMatcher.create(pattern, "abc123");
} catch (TimeLimitedMatcher.TimeoutException e) {
// handle exception
}
if (matcher.matches()) {
// do something with the match
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classPublic, checked exception that is thrown when RegEx times out. -
Method Summary
Modifier and TypeMethodDescriptionstatic TimeLimitedMatchercreate(Pattern pattern, CharSequence inputString) Creates a new Time-limited matcher instance to search for the regular expressionpatternagainst theinputString.static TimeLimitedMatchercreate(Pattern pattern, CharSequence inputString, Duration duration) Creates a new Time-limited matcher instance to search for the regular expressionpatternagainst theinputString.group()Returns the input subsequence matched by the previous match.group(int group) Returns the input subsequence captured by the given group during the previous match operation.Returns the input subsequence captured by the given named-capturing group during the previous match operation.intReturns the number of capturing groups in this matcher's pattern.booleanmatches()True if the entire region matched against the pattern.toString()
-
Method Details
-
create
public static TimeLimitedMatcher create(Pattern pattern, CharSequence inputString) throws TimeLimitedMatcher.TimeoutException Creates a new Time-limited matcher instance to search for the regular expressionpatternagainst theinputString.This method executes the search before returning, throwing a
TimeLimitedMatcher.TimeoutExceptionif the RegEx search runs longer thanDEFAULT_DURATION.- Parameters:
pattern- RegEx search patterninputString- String to match against.- Returns:
- TimeLimitedMatcher instance to use for reporting out match details
- Throws:
TimeLimitedMatcher.TimeoutException- if the RegEx search runs longer thanDEFAULT_DURATION
-
create
public static TimeLimitedMatcher create(Pattern pattern, CharSequence inputString, Duration duration) throws TimeLimitedMatcher.TimeoutException Creates a new Time-limited matcher instance to search for the regular expressionpatternagainst theinputString.This method executes the search before returning, throwing a
TimeoutExceptionif the RegEx search runs longer thanduration.- Parameters:
pattern- RegEx search patterninputString- String to match against.duration- the length of time to continue processing the RegEx before failing- Returns:
- TimeLimitedMatcher instance to use for reporting out match details
- Throws:
TimeLimitedMatcher.TimeoutException- if the RegEx search runs longer thanduration
-
group
Returns the input subsequence matched by the previous match.- Returns:
- The (possibly empty) subsequence matched by the previous match, in string form
- See Also:
-
group
Returns the input subsequence captured by the given group during the previous match operation.- Parameters:
group- The index of a capturing group in this matcher's pattern- Returns:
- The (possibly empty) subsequence captured by the group during the previous match, or null if the group failed to match part of the input
- See Also:
-
group
Returns the input subsequence captured by the given named-capturing group during the previous match operation.- Parameters:
name- The name of a named-capturing group in this matcher's pattern- Returns:
- The (possibly empty) subsequence captured by the named group during the previous match, or null if the group failed to match part of the input
- See Also:
-
groupCount
public int groupCount()Returns the number of capturing groups in this matcher's pattern.- Returns:
- The number of capturing groups in this matcher's pattern
- See Also:
-
matches
public boolean matches()True if the entire region matched against the pattern.- Returns:
- true if the entire region matched against the pattern.
- See Also:
-
toString
-