Class Perl5Matcher
- java.lang.Object
-
- org.apache.oro.text.regex.Perl5Matcher
-
- All Implemented Interfaces:
PatternMatcher
public final class Perl5Matcher extends java.lang.Object implements PatternMatcher
The Perl5Matcher class is used to match regular expressions (conforming to the Perl5 regular expression syntax) generated by Perl5Compiler.Perl5Compiler and Perl5Matcher are designed with the intent that you use a separate instance of each per thread to avoid the overhead of both synchronization and concurrent access (e.g., a match that takes a long time in one thread will block the progress of another thread with a shorter match). If you want to use a single instance of each in a concurrent program, you must appropriately protect access to the instances with critical sections. If you want to share Perl5Pattern instances between concurrently executing instances of Perl5Matcher, you must compile the patterns with
Perl5Compiler.READ_ONLY_MASK.- Since:
- 1.0
- See Also:
PatternMatcher,Perl5Compiler
-
-
Field Summary
Fields Modifier and Type Field Description private int[]__beginMatchOffsetsprivate int__bolprivate boolean__caseInsensitiveprivate int__currentOffsetprivate Perl5Repetition__currentRepprivate static int__DEFAULT_LAST_MATCH_END_OFFSETprivate int[]__endMatchOffsetsprivate int__endOffsetprivate int__eolprivate static char__EOSprivate int__expSizeprivate static int__INITIAL_NUM_OFFSETSprivate char[]__inputprivate int__inputOffsetprivate int__lastMatchInputEndOffsetprivate Perl5MatchResult__lastMatchResultprivate int__lastParenprivate boolean__lastSuccessprivate boolean__multilineprivate int__numParenthesesprivate char[]__originalInputprivate char__previousCharprivate char[]__programprivate java.util.Stack<int[]>__stack
-
Constructor Summary
Constructors Constructor Description Perl5Matcher()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static boolean__compare(char[] s1, int s1off, char[] s2, int s2off, int n)private static int__findFirst(char[] input, int curr, int endOffset, char[] mustString)private void__initInterpreterGlobals(Perl5Pattern expression, char[] input, int beginOffset, int endOff, int currentOffset)private boolean__interpret(Perl5Pattern expression, char[] input, int beginOffset, int endOff, int currentOffset)private boolean__match(int offset)private boolean__matchUnicodeClass(char code, char[] __program1, int off, char opcode)private void__popState()private void__pushState(int parenFloor)private int__repeat(int offset, int max)private void__setLastMatchResult()private boolean__tryExpression(int offset)(package private) static char[]_toLower(char[] in)booleancontains(char[] in, Pattern pattern)Determines if a string (represented as a char[]) contains a pattern.booleancontains(java.lang.String input, Pattern pattern)Determines if a string contains a pattern.booleancontains(PatternMatcherInput input, Pattern pattern)Determines if the contents of a PatternMatcherInput, starting from the current offset of the input contains a pattern.MatchResultgetMatch()Fetches the last match found by a call to a matches() or contains() method.booleanmatches(char[] in, Pattern pattern)Determines if a string (represented as a char[]) exactly matches a given pattern.booleanmatches(java.lang.String input, Pattern pattern)Determines if a string exactly matches a given pattern.booleanmatches(PatternMatcherInput input, Pattern pattern)Determines if the contents of a PatternMatcherInput instance exactly matches a given pattern.booleanmatchesPrefix(char[] input, Pattern pattern)Determines if a prefix of a string (represented as a char[]) matches a given pattern.booleanmatchesPrefix(char[] in, Pattern pattern, int offset)Determines if a prefix of a string (represented as a char[]) matches a given pattern, starting from a given offset into the string.booleanmatchesPrefix(java.lang.String input, Pattern pattern)Determines if a prefix of a string matches a given pattern.booleanmatchesPrefix(PatternMatcherInput input, Pattern pattern)Determines if a prefix of a PatternMatcherInput instance matches a given pattern.
-
-
-
Field Detail
-
__EOS
private static final char __EOS
- See Also:
- Constant Field Values
-
__INITIAL_NUM_OFFSETS
private static final int __INITIAL_NUM_OFFSETS
- See Also:
- Constant Field Values
-
__multiline
private final boolean __multiline
- See Also:
- Constant Field Values
-
__lastSuccess
private boolean __lastSuccess
-
__caseInsensitive
private boolean __caseInsensitive
-
__previousChar
private char __previousChar
-
__input
private char[] __input
-
__originalInput
private char[] __originalInput
-
__currentRep
private Perl5Repetition __currentRep
-
__numParentheses
private int __numParentheses
-
__bol
private int __bol
-
__eol
private int __eol
-
__currentOffset
private int __currentOffset
-
__endOffset
private int __endOffset
-
__program
private char[] __program
-
__expSize
private int __expSize
-
__inputOffset
private int __inputOffset
-
__lastParen
private int __lastParen
-
__beginMatchOffsets
private int[] __beginMatchOffsets
-
__endMatchOffsets
private int[] __endMatchOffsets
-
__stack
private final java.util.Stack<int[]> __stack
-
__lastMatchResult
private Perl5MatchResult __lastMatchResult
-
__DEFAULT_LAST_MATCH_END_OFFSET
private static final int __DEFAULT_LAST_MATCH_END_OFFSET
- See Also:
- Constant Field Values
-
__lastMatchInputEndOffset
private int __lastMatchInputEndOffset
-
-
Method Detail
-
__compare
private static boolean __compare(char[] s1, int s1off, char[] s2, int s2off, int n)
-
__findFirst
private static int __findFirst(char[] input, int curr, int endOffset, char[] mustString)
-
__pushState
private void __pushState(int parenFloor)
-
__popState
private void __popState()
-
__initInterpreterGlobals
private void __initInterpreterGlobals(Perl5Pattern expression, char[] input, int beginOffset, int endOff, int currentOffset)
-
__setLastMatchResult
private void __setLastMatchResult()
-
__interpret
private boolean __interpret(Perl5Pattern expression, char[] input, int beginOffset, int endOff, int currentOffset)
-
__matchUnicodeClass
private boolean __matchUnicodeClass(char code, char[] __program1, int off, char opcode)
-
__tryExpression
private boolean __tryExpression(int offset)
-
__repeat
private int __repeat(int offset, int max)
-
__match
private boolean __match(int offset)
-
_toLower
static char[] _toLower(char[] in)
-
matchesPrefix
public boolean matchesPrefix(char[] in, Pattern pattern, int offset)Determines if a prefix of a string (represented as a char[]) matches a given pattern, starting from a given offset into the string. If a prefix of the string matches the pattern, a MatchResult instance representing the match is made accesible viagetMatch().This method is useful for certain common token identification tasks that are made more difficult without this functionality.
- Specified by:
matchesPrefixin interfacePatternMatcher- Parameters:
in- The char[] to test for a prefix match.pattern- The Pattern to be matched.offset- The offset at which to start searching for the prefix.- Returns:
- True if input matches pattern, false otherwise.
-
matchesPrefix
public boolean matchesPrefix(char[] input, Pattern pattern)Determines if a prefix of a string (represented as a char[]) matches a given pattern. If a prefix of the string matches the pattern, a MatchResult instance representing the match is made accesible viagetMatch().This method is useful for certain common token identification tasks that are made more difficult without this functionality.
- Specified by:
matchesPrefixin interfacePatternMatcher- Parameters:
input- The char[] to test for a prefix match.pattern- The Pattern to be matched.- Returns:
- True if input matches pattern, false otherwise.
-
matchesPrefix
public boolean matchesPrefix(java.lang.String input, Pattern pattern)Determines if a prefix of a string matches a given pattern. If a prefix of the string matches the pattern, a MatchResult instance representing the match is made accesible viagetMatch().This method is useful for certain common token identification tasks that are made more difficult without this functionality.
- Specified by:
matchesPrefixin interfacePatternMatcher- Parameters:
input- The String to test for a prefix match.pattern- The Pattern to be matched.- Returns:
- True if input matches pattern, false otherwise.
-
matchesPrefix
public boolean matchesPrefix(PatternMatcherInput input, Pattern pattern)
Determines if a prefix of a PatternMatcherInput instance matches a given pattern. If there is a match, a MatchResult instance representing the match is made accesible viagetMatch(). Unlike thecontains(PatternMatcherInput, Pattern)method, the current offset of the PatternMatcherInput argument is not updated. However, unlike thematches(PatternMatcherInput, Pattern)method, matchesPrefix() will start its search from the current offset rather than the begin offset of the PatternMatcherInput.This method is useful for certain common token identification tasks that are made more difficult without this functionality.
- Specified by:
matchesPrefixin interfacePatternMatcher- Parameters:
input- The PatternMatcherInput to test for a prefix match.pattern- The Pattern to be matched.- Returns:
- True if input matches pattern, false otherwise.
-
matches
public boolean matches(char[] in, Pattern pattern)Determines if a string (represented as a char[]) exactly matches a given pattern. If there is an exact match, a MatchResult instance representing the match is made accesible viagetMatch(). The pattern must be a Perl5Pattern instance, otherwise a ClassCastException will be thrown. You are not required to, and indeed should NOT try to (for performance reasons), catch a ClassCastException because it will never be thrown as long as you use a Perl5Pattern as the pattern parameter.Note: matches() is not the same as sticking a ^ in front of your expression and a $ at the end of your expression in Perl5 and using the =~ operator, even though in many cases it will be equivalent. matches() literally looks for an exact match according to the rules of Perl5 expression matching. Therefore, if you have a pattern foo|foot and are matching the input foot it will not produce an exact match. But foot|foo will produce an exact match for either foot or foo. Remember, Perl5 regular expressions do not match the longest possible match. From the perlre manpage:
Alternatives are tried from left to right, so the first alternative found for which the entire expression matches, is the one that is chosen. This means that alternatives are not necessarily greedy. For example: when matching foo|foot against "barefoot", only the "foo" part will match, as that is the first alternative tried, and it successfully matches the target string.
- Specified by:
matchesin interfacePatternMatcher- Parameters:
in- The char[] to test for an exact match.pattern- The Perl5Pattern to be matched.- Returns:
- True if input matches pattern, false otherwise.
- Throws:
java.lang.ClassCastException- If a Pattern instance other than a Perl5Pattern is passed as the pattern parameter.
-
matches
public boolean matches(java.lang.String input, Pattern pattern)Determines if a string exactly matches a given pattern. If there is an exact match, a MatchResult instance representing the match is made accesible viagetMatch(). The pattern must be a Perl5Pattern instance, otherwise a ClassCastException will be thrown. You are not required to, and indeed should NOT try to (for performance reasons), catch a ClassCastException because it will never be thrown as long as you use a Perl5Pattern as the pattern parameter.Note: matches() is not the same as sticking a ^ in front of your expression and a $ at the end of your expression in Perl5 and using the =~ operator, even though in many cases it will be equivalent. matches() literally looks for an exact match according to the rules of Perl5 expression matching. Therefore, if you have a pattern foo|foot and are matching the input foot it will not produce an exact match. But foot|foo will produce an exact match for either foot or foo. Remember, Perl5 regular expressions do not match the longest possible match. From the perlre manpage:
Alternatives are tried from left to right, so the first alternative found for which the entire expression matches, is the one that is chosen. This means that alternatives are not necessarily greedy. For example: when matching foo|foot against "barefoot", only the "foo" part will match, as that is the first alternative tried, and it successfully matches the target string.
- Specified by:
matchesin interfacePatternMatcher- Parameters:
input- The String to test for an exact match.pattern- The Perl5Pattern to be matched.- Returns:
- True if input matches pattern, false otherwise.
- Throws:
java.lang.ClassCastException- If a Pattern instance other than a Perl5Pattern is passed as the pattern parameter.
-
matches
public boolean matches(PatternMatcherInput input, Pattern pattern)
Determines if the contents of a PatternMatcherInput instance exactly matches a given pattern. If there is an exact match, a MatchResult instance representing the match is made accesible viagetMatch(). Unlike thecontains(PatternMatcherInput, Pattern)method, the current offset of the PatternMatcherInput argument is not updated. You should remember that the region between the begin (NOT the current) and end offsets of the PatternMatcherInput will be tested for an exact match.The pattern must be a Perl5Pattern instance, otherwise a ClassCastException will be thrown. You are not required to, and indeed should NOT try to (for performance reasons), catch a ClassCastException because it will never be thrown as long as you use a Perl5Pattern as the pattern parameter.
Note: matches() is not the same as sticking a ^ in front of your expression and a $ at the end of your expression in Perl5 and using the =~ operator, even though in many cases it will be equivalent. matches() literally looks for an exact match according to the rules of Perl5 expression matching. Therefore, if you have a pattern foo|foot and are matching the input foot it will not produce an exact match. But foot|foo will produce an exact match for either foot or foo. Remember, Perl5 regular expressions do not match the longest possible match. From the perlre manpage:
Alternatives are tried from left to right, so the first alternative found for which the entire expression matches, is the one that is chosen. This means that alternatives are not necessarily greedy. For example: when matching foo|foot against "barefoot", only the "foo" part will match, as that is the first alternative tried, and it successfully matches the target string.
- Specified by:
matchesin interfacePatternMatcher- Parameters:
input- The PatternMatcherInput to test for a match.pattern- The Perl5Pattern to be matched.- Returns:
- True if input matches pattern, false otherwise.
- Throws:
java.lang.ClassCastException- If a Pattern instance other than a Perl5Pattern is passed as the pattern parameter.
-
contains
public boolean contains(java.lang.String input, Pattern pattern)Determines if a string contains a pattern. If the pattern is matched by some substring of the input, a MatchResult instance representing the first such match is made acessible viagetMatch(). If you want to access subsequent matches you should either use a PatternMatcherInput object or use the offset information in the MatchResult to create a substring representing the remaining input. Using the MatchResult offset information is the recommended method of obtaining the parts of the string preceeding the match and following the match.The pattern must be a Perl5Pattern instance, otherwise a ClassCastException will be thrown. You are not required to, and indeed should NOT try to (for performance reasons), catch a ClassCastException because it will never be thrown as long as you use a Perl5Pattern as the pattern parameter.
- Specified by:
containsin interfacePatternMatcher- Parameters:
input- The String to test for a match.pattern- The Perl5Pattern to be matched.- Returns:
- True if the input contains a pattern match, false otherwise.
- Throws:
java.lang.ClassCastException- If a Pattern instance other than a Perl5Pattern is passed as the pattern parameter.
-
contains
public boolean contains(char[] in, Pattern pattern)Determines if a string (represented as a char[]) contains a pattern. If the pattern is matched by some substring of the input, a MatchResult instance representing the first such match is made acessible viagetMatch(). If you want to access subsequent matches you should either use a PatternMatcherInput object or use the offset information in the MatchResult to create a substring representing the remaining input. Using the MatchResult offset information is the recommended method of obtaining the parts of the string preceeding the match and following the match.The pattern must be a Perl5Pattern instance, otherwise a ClassCastException will be thrown. You are not required to, and indeed should NOT try to (for performance reasons), catch a ClassCastException because it will never be thrown as long as you use a Perl5Pattern as the pattern parameter.
- Specified by:
containsin interfacePatternMatcher- Parameters:
in- The char[] to test for a match.pattern- The Perl5Pattern to be matched.- Returns:
- True if the input contains a pattern match, false otherwise.
- Throws:
java.lang.ClassCastException- If a Pattern instance other than a Perl5Pattern is passed as the pattern parameter.
-
contains
public boolean contains(PatternMatcherInput input, Pattern pattern)
Determines if the contents of a PatternMatcherInput, starting from the current offset of the input contains a pattern. If a pattern match is found, a MatchResult instance representing the first such match is made acessible viagetMatch(). The current offset of the PatternMatcherInput is set to the offset corresponding to the end of the match, so that a subsequent call to this method will continue searching where the last call left off. You should remember that the region between the begin and end offsets of the PatternMatcherInput are considered the input to be searched, and that the current offset of the PatternMatcherInput reflects where a search will start from. Matches extending beyond the end offset of the PatternMatcherInput will not be matched. In other words, a match must occur entirely between the begin and end offsets of the input. SeePatternMatcherInputfor more details.As a side effect, if a match is found, the PatternMatcherInput match offset information is updated. See the
PatternMatcherInput.setMatchOffsets(int, int)method for more details.The pattern must be a Perl5Pattern instance, otherwise a ClassCastException will be thrown. You are not required to, and indeed should NOT try to (for performance reasons), catch a ClassCastException because it will never be thrown as long as you use a Perl5Pattern as the pattern parameter.
This method is usually used in a loop as follows:
PatternMatcher matcher; PatternCompiler compiler; Pattern pattern; PatternMatcherInput input; MatchResult result; compiler = new Perl5Compiler(); matcher = new Perl5Matcher(); try { pattern = compiler.compile(somePatternString); } catch (MalformedPatternException e) { System.err.println("Bad pattern."); System.err.println(e.getMessage()); return; } input = new PatternMatcherInput(someStringInput); while (matcher.contains(input, pattern)) { result = matcher.getMatch(); // Perform whatever processing on the result you want. }- Specified by:
containsin interfacePatternMatcher- Parameters:
input- The PatternMatcherInput to test for a match.pattern- The Pattern to be matched.- Returns:
- True if the input contains a pattern match, false otherwise.
- Throws:
java.lang.ClassCastException- If a Pattern instance other than a Perl5Pattern is passed as the pattern parameter.
-
getMatch
public MatchResult getMatch()
Fetches the last match found by a call to a matches() or contains() method. If you plan on modifying the original search input, you must call this method BEFORE you modify the original search input, as a lazy evaluation technique is used to create the MatchResult. This reduces the cost of pattern matching when you don't care about the actual match and only care if the pattern occurs in the input. Otherwise, a MatchResult would be created for every match found, whether or not the MatchResult was later used by a call to getMatch().- Specified by:
getMatchin interfacePatternMatcher- Returns:
- A MatchResult instance containing the pattern match found by the last call to any one of the matches() or contains() methods. If no match was found by the last call, returns null.
-
-