Package dk.brics.automaton
Class MatchOnlyRunAutomaton
java.lang.Object
dk.brics.automaton.MatchOnlyRunAutomaton
- All Implemented Interfaces:
Serializable
Supports an optimised version of substring matching an automaton against an input string in linear
time with no backtracking.
In essence we simply take an automaton "mypattern" and match against ".*mypattern". See the docs here from Russ Cox: Link.
As such we do not currently support identifying the start, end position of a match, simply that a match exists.
Execution time is linear to the length of the string O(n) for the method matches(CharSequence) below.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic MatchOnlyRunAutomatonload(InputStream stream) Retrieves a serializedMatchOnlyRunAutomatonfrom a stream.booleanmatches(CharSequence input) Search the input in linear time based on the size of the input to identify whether this automaton is contained anywhere in the input.booleanmatches(CharSequence input, int pos) Search the input in linear time based on the size of the input to identify whether this pattern is contained anywhere in the input from a starting position.voidstore(OutputStream stream) Writes thisMatchOnlyRunAutomatonto the given stream.toString()
-
Constructor Details
-
MatchOnlyRunAutomaton
-
MatchOnlyRunAutomaton
-
-
Method Details
-
matches
Search the input in linear time based on the size of the input to identify whether this automaton is contained anywhere in the input.Has the following constraints:
- In the worst case, the execution is equivalent to the length of the input O(n) as we scan the whole input.
- There is no backtracking, we simply scan the input char by char until we either match or reach the end.
- Method returns immediately if it finds enough chars to complete a match of the pattern.
- The method and class has no ability to locate the position of the match, simply that is either exists or not.
- Parameters:
input- the input to match a substring pattern against- Returns:
trueif the pattern is found in a substring in the input, orfalseif the input is not matched. We also return false for null inputs.
-
matches
Search the input in linear time based on the size of the input to identify whether this pattern is contained anywhere in the input from a starting position.See
matches(CharSequence)for more information.This version of the method searches from a specific starting position.
- Parameters:
input- the input to match a substring pattern againstpos- the position to start searching from- Returns:
trueif the pattern is found in a substring in the input, orfalseif the input is not matched. We also return false for null inputs- Throws:
IllegalArgumentException- if the pos is negative or larger than the input length.
-
load
public static MatchOnlyRunAutomaton load(InputStream stream) throws IOException, ClassCastException, ClassNotFoundException Retrieves a serializedMatchOnlyRunAutomatonfrom a stream.- Parameters:
stream- input stream with serialized automaton- Throws:
IOException- if input/output related exception occursClassCastException- if the data is not a serializedMatchOnlyRunAutomatonClassNotFoundException- if the class of the serialized object cannot be found
-
store
Writes thisMatchOnlyRunAutomatonto the given stream.- Parameters:
stream- output stream for serialized automaton- Throws:
IOException- if input/output related exception occurs
-
toString
-