public final class RegexSpanFinder 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.
Proper usage is:
try ( RegexSpanFinder finder = new RegexSpanFinder( "\\s+" ) ) {
final List
Constructor and Description |
---|
RegexSpanFinder(Pattern pattern)
Uses the default timeout of 1000 milliseconds
|
RegexSpanFinder(Pattern pattern,
int timeoutMillis)
Uses the default timeout of 1000 milliseconds
|
RegexSpanFinder(String regex)
Uses the default timeout of 1000 milliseconds
|
RegexSpanFinder(String regex,
int timeoutMillis) |
Modifier and Type | Method and Description |
---|---|
void |
close()
shut down the executor
|
List<Pair<Integer>> |
findSpans(String text) |
public RegexSpanFinder(String regex) throws IllegalArgumentException
regex
- regular expressionIllegalArgumentException
- if the regular expression is null or malformedpublic RegexSpanFinder(String regex, int timeoutMillis) throws IllegalArgumentException
regex
- regular expressiontimeoutMillis
- milliseconds at which the regex match should abort, between 100 and 10000IllegalArgumentException
- if the regular expression is null or malformedpublic RegexSpanFinder(Pattern pattern) throws IllegalArgumentException
pattern
- Pattern compiled from a regular expressionIllegalArgumentException
- if the pattern is null or malformedpublic RegexSpanFinder(Pattern pattern, int timeoutMillis) throws IllegalArgumentException
pattern
- Pattern compiled from a regular expressiontimeoutMillis
- milliseconds at which the regex match should abort, between 100 and 10000IllegalArgumentException
- if the pattern is null or malformedpublic List<Pair<Integer>> findSpans(String text)
text
- text in which a find should be conductedpublic void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2012-2017 The Apache Software Foundation. All Rights Reserved.