All Packages Class Hierarchy This Package Previous Next Index
Class com.oroinc.text.regex.Perl5Matcher
java.lang.Object
|
+----com.oroinc.text.regex.Perl5Matcher
- public final class Perl5Matcher
- extends Object
- implements PatternMatcher
The Perl5Matcher class is used to match regular expressions
(conforming to the Perl5 regular expression syntax) generated by
Perl5Compiler.
Copyright © 1997-1998 Original Resuable Objects, Inc.
All rights reserved.
- Author:
- Daniel F. Savarese
- See Also:
- PatternMatcher, Perl5Compiler
-
Perl5Matcher()
-
-
contains(char[], Pattern)
- Determines if a string (represented as a char[]) contains a pattern.
-
contains(PatternMatcherInput, Pattern)
- Determines if the contents of a PatternMatcherInput, starting from the
current offset of the input contains a pattern.
-
contains(Perl5StreamInput, Perl5Pattern)
- Determines if the contents of a Perl5StreamInput, starting from the
current offset of the input contains a pattern.
-
contains(String, Pattern)
- Determines if a string contains a pattern.
-
getMatch()
- Fetches the last match found by a call to a matches() or contains()
method.
-
isMultiline()
-
-
matches(char[], Pattern)
- Determines if a string (represented as a char[]) exactly
matches a given pattern.
-
matches(PatternMatcherInput, Pattern)
- Determines if the contents of a PatternMatcherInput instance
exactly matches a given pattern.
-
matches(String, Pattern)
- Determines if a string exactly matches a given pattern.
-
matchesPrefix(char[], Pattern)
- Determines if a prefix of a string (represented as a char[])
matches a given pattern.
-
matchesPrefix(char[], Pattern, int)
- Determines if a prefix of a string (represented as a char[])
matches a given pattern, starting from a given offset into the string.
-
matchesPrefix(PatternMatcherInput, Pattern)
- Determines if a prefix of a PatternMatcherInput instance
matches a given pattern.
-
matchesPrefix(String, Pattern)
- Determines if a prefix of a string matches a given pattern.
-
setMultiline(boolean)
- Set whether or not subsequent calls to matches()
or contains() should treat the input as
consisting of multiple lines.
Perl5Matcher
public Perl5Matcher()
setMultiline
public void setMultiline(boolean multiline)
- Set whether or not subsequent calls to matches()
or contains() should treat the input as
consisting of multiple lines. The default behavior is for
input to be treated as consisting of multiple lines. This method
should only be called if the Perl5Pattern used for a match was
compiled without either of the Perl5Compiler.MULTILINE_MASK or
Perl5Compiler.SINGLELINE_MASK flags, and you want to alter the
behavior of how the ^, $, and . metacharacters are
interpreted on the fly. The compilation options used when compiling
a pattern ALWAYS override the behavior specified by setMultiline(). See
Perl5Compiler
for more details.
- Parameters:
- multiline - If set to true treats the input as consisting of
multiple lines with respect to the ^ and $
metacharacters. If set to false treats the input as consisting
of a single line with respect to the ^ and $
metacharacters.
isMultiline
public boolean isMultiline()
- Returns:
- True if the matcher is treating input as consisting of multiple
lines with respect to the ^ and $ metacharacters,
false otherwise.
matchesPrefix
public boolean matchesPrefix(char input[],
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 via
getMatch() .
This method is useful for certain common token identification tasks
that are made more difficult without this functionality.
- Parameters:
- input - 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 via
getMatch() .
This method is useful for certain common token identification tasks
that are made more difficult without this functionality.
- 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(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 via
getMatch() .
This method is useful for certain common token identification tasks
that are made more difficult without this functionality.
- 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 via
getMatch() . Unlike the
contains(PatternMatcherInput, Pattern)
method, the current offset of the PatternMatcherInput argument
is not updated. However, unlike the 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.
- 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 input[],
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 via
getMatch() . 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.
- Parameters:
- input - The char[] to test for an exact match.
- pattern - The Perl5Pattern to be matched.
- Returns:
- True if input matches pattern, false otherwise.
- Throws: ClassCastException
- If a Pattern instance other than a
Perl5Pattern is passed as the pattern parameter.
matches
public boolean matches(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 via
getMatch() . 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.
- 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: 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 via
getMatch() . Unlike the
contains(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.
- Parameters:
- input - The PatternMatcherInput to test for a match.
- pattern - The Perl5Pattern to be matched.
- Returns:
- True if input matches pattern, false otherwise.
- Throws: ClassCastException
- If a Pattern instance other than a
Perl5Pattern is passed as the pattern parameter.
contains
public boolean contains(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 via
getMatch() . 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.
- 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: ClassCastException
- If a Pattern instance other than a
Perl5Pattern is passed as the pattern parameter.
contains
public boolean contains(char input[],
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 via
getMatch() . 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.
- Parameters:
- input - 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: 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 via
getMatch() . 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. See
PatternMatcherInput for 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.
}
- 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: ClassCastException
- If a Pattern instance other than a
Perl5Pattern is passed as the pattern parameter.
contains
public boolean contains(Perl5StreamInput input,
Perl5Pattern pattern) throws IOException
- Determines if the contents of a Perl5StreamInput, 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 via
getMatch() . The current offset of the
stream is advanced to the end offset corresponding to the end
of the match. Consequently a subsequent call to this method will continue
searching where the last call left off.
See
Perl5StreamInput for more details.
Note, patterns matching the null string do NOT match at end of input
stream. This is different from the behavior you get from the other
contains() methods.
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;
Perl5StreamInput 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 Perl5StreamInput(
new BufferedReader(new FileReader(someFileName)))
while(matcher.contains(input, pattern)) {
result = matcher.getMatch();
// Perform whatever processing on the result you want.
}
- Parameters:
- input - The Perl5StreamInput to test for a match.
- pattern - The Pattern to be matched.
- Returns:
- True if the input contains a pattern match, false otherwise.
- Throws: 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().
- 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.
All Packages Class Hierarchy This Package Previous Next Index