public class TimeoutMatcher extends Object implements Closeable
Matcher.find()
in a separate thread so that it may be interrupted at a set timeout.
This prevents infinite loop problems that can be caused by poorly-built expressions or unexpected text contents.
The timeout can be specified in milliseconds between 100 and 10,000. Large timeouts are unadvised. If a large
amount of text needs to be parsed then it is better to split up the text logically and use smaller timeouts.
The default timeout is 1000 milliseconds.
Extending Matcher would be better, but it is final
Proper usage is: try ( TimeoutMatcher finder = new TimeoutMatcher( "\\s+", "Hello World !" ) ) { Matcher matcher = finder.find(); while ( matcher != null ) { ... matcher = finder.find(); } } catch ( IllegalArgumentException iaE ) { ... }
Constructor and Description |
---|
TimeoutMatcher(Pattern pattern,
String text)
Uses the default timeout of 1000 milliseconds
|
TimeoutMatcher(Pattern pattern,
String text,
int timeoutMillis)
Uses the default timeout of 1000 milliseconds
|
TimeoutMatcher(String regex,
String text)
Uses the default timeout of 1000 milliseconds
|
TimeoutMatcher(String regex,
String text,
int timeoutMillis) |
public TimeoutMatcher(String regex, String text) throws IllegalArgumentException
regex
- regular expressiontext
- text to parseIllegalArgumentException
- if the regular expression is null or malformedpublic TimeoutMatcher(String regex, String text, int timeoutMillis) throws IllegalArgumentException
regex
- regular expressiontext
- text to parsetimeoutMillis
- milliseconds at which the regex match should abort, between 100 and 10000IllegalArgumentException
- if the regular expression is null or malformedpublic TimeoutMatcher(Pattern pattern, String text) throws IllegalArgumentException
pattern
- Pattern compiled from a regular expressiontext
- text to parseIllegalArgumentException
- if the pattern is null or malformedpublic TimeoutMatcher(Pattern pattern, String text, int timeoutMillis) throws IllegalArgumentException
pattern
- Pattern compiled from a regular expressiontext
- text to parsetimeoutMillis
- milliseconds at which the regex match should abort, between 100 and 10000IllegalArgumentException
- if the pattern is null or malformedpublic Matcher nextMatch()
Matcher.find()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2012-2017 The Apache Software Foundation. All Rights Reserved.