Methods Summary |
---|
private void | appendEvaluated(java.lang.StringBuffer buffer, java.lang.String s)Internal helper method to append a given string to a given string buffer.
If the string contains any references to groups, these are replaced by
the corresponding group's contents.
boolean escape = false;
boolean dollar = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '\\" && !escape) {
escape = true;
} else if (c == '$" && !escape) {
dollar = true;
} else if (c >= '0" && c <= '9" && dollar) {
buffer.append(group(c - '0"));
dollar = false;
} else {
buffer.append(c);
dollar = false;
escape = false;
}
}
// This seemingly stupid piece of code reproduces a JDK bug.
if (escape) {
throw new ArrayIndexOutOfBoundsException(s.length());
}
|
public java.util.regex.Matcher | appendReplacement(java.lang.StringBuffer buffer, java.lang.String replacement)Appends a literal part of the input plus a replacement for the current
match to a given {@link StringBuffer}. The literal part is exactly the
part of the input between the previous match and the current match. The
method can be used in conjunction with {@link #find()} and
{@link #appendTail(StringBuffer)} to walk through the input and replace
all occurrences of the {@code Pattern} with something else.
buffer.append(input.substring(appendPos, start()));
appendEvaluated(buffer, replacement);
appendPos = end();
return this;
|
public java.lang.StringBuffer | appendTail(java.lang.StringBuffer buffer)Appends the (unmatched) remainder of the input to the given
{@link StringBuffer}. The method can be used in conjunction with
{@link #find()} and {@link #appendReplacement(StringBuffer, String)} to
walk through the input and replace all matches of the {@code Pattern}
with something else.
if (appendPos < regionEnd) {
buffer.append(input.substring(appendPos, regionEnd));
}
return buffer;
|
public int | end()Returns the index of the first character following the text that matched
the whole regular expression.
return end(0);
|
public int | end(int group)Returns the index of the first character following the text that matched
a given group.
ensureMatch();
return matchOffsets[(group * 2) + 1];
|
private void | ensureMatch()Makes sure that a successful match has been made. Is invoked internally
from various places in the class.
if (!matchFound) {
throw new IllegalStateException("No successful match so far");
}
|
protected void | finalize()
try {
if (nativePattern != 0) {
NativeRegEx.close(nativePattern);
}
}
finally {
super.finalize();
}
|
public boolean | find()Returns the next occurrence of the {@link Pattern} in the input. If a
previous match was successful, the method continues the search from the
first character following that match in the input. Otherwise it searches
either from the region start (if one has been set), or from position 0.
if (!searching) {
searching = true;
matchFound = NativeRegEx.find(nativePattern, -1);
} else {
matchFound = NativeRegEx.findNext(nativePattern);
}
if (matchFound) {
NativeRegEx.startEnd(nativePattern, matchOffsets);
findPos = matchOffsets[1];
}
return matchFound;
|
public boolean | find(int start)Returns the next occurrence of the {@link Pattern} in the input. The
method starts the search from the given character in the input.
findPos = start;
if (findPos < regionStart) {
findPos = regionStart;
} else if (findPos >= regionEnd) {
matchFound = false;
return false;
}
matchFound = NativeRegEx.find(nativePattern, findPos);
if (matchFound) {
NativeRegEx.startEnd(nativePattern, matchOffsets);
findPos = matchOffsets[1];
}
return matchFound;
|
public java.lang.String | group()Returns the text that matched the whole regular expression.
return group(0);
|
public java.lang.String | group(int group)Returns the text that matched a given group of the regular expression.
ensureMatch();
int from = matchOffsets[group * 2];
int to = matchOffsets[(group * 2) + 1];
if (from == -1 || to == -1) {
return null;
} else {
return input.substring(from, to);
}
|
public int | groupCount()Returns the number of groups in the results, which is always equal to
the number of groups in the original regular expression.
return pattern.mGroupCount;
|
public boolean | hasAnchoringBounds()Indicates whether this matcher has anchoring bounds enabled. When
anchoring bounds are enabled, the start and end of the input match the
'^' and '$' meta-characters, otherwise not. Anchoring bounds are enabled
by default.
return anchoringBounds;
|
public boolean | hasTransparentBounds()Indicates whether this matcher has transparent bounds enabled. When
transparent bounds are enabled, the parts of the input outside the region
are subject to lookahead and lookbehind, otherwise they are not.
Transparent bounds are disabled by default.
return transparentBounds;
|
public boolean | hitEnd()Indicates whether the last match hit the end of the input.
return NativeRegEx.hitEnd(nativePattern);
|
public boolean | lookingAt()Tries to match the {@link Pattern}, starting from the beginning of the
region (or the beginning of the input, if no region has been set).
Doesn't require the {@code Pattern} to match against the whole region.
matchFound = NativeRegEx.lookingAt(nativePattern, -1);
if (matchFound) {
NativeRegEx.startEnd(nativePattern, matchOffsets);
findPos = matchOffsets[1];
}
return matchFound;
|
public boolean | matches()Tries to match the {@link Pattern} against the entire region (or the
entire input, if no region has been set).
matchFound = NativeRegEx.matches(nativePattern, -1);
if (matchFound) {
NativeRegEx.startEnd(nativePattern, matchOffsets);
findPos = matchOffsets[1];
}
return matchFound;
|
public java.util.regex.Pattern | pattern()Returns the {@link Pattern} instance used inside this matcher.
return pattern;
|
public static java.lang.String | quoteReplacement(java.lang.String s)Returns a replacement string for the given one that has all backslashes
and dollar signs escaped.
StringBuffer buffer = new StringBuffer(s.length());
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '\\" || c == '$") {
buffer.append('\\");
}
buffer.append(c);
}
return buffer.toString();
|
public java.util.regex.Matcher | region(int start, int end)Resets this matcher and sets a region. Only characters inside the region
are considered for a match.
return reset(input, start, end);
|
public int | regionEnd()Returns this matcher's region end, that is, the first character that is
not considered for a match.
return regionEnd;
|
public int | regionStart()Returns this matcher's region start, that is, the first character that is
considered for a match.
return regionStart;
|
public java.lang.String | replaceAll(java.lang.String replacement)Replaces all occurrences of this matcher's pattern in the input with a
given string.
StringBuffer buffer = new StringBuffer(input.length());
findPos = 0;
appendPos = 0;
matchFound = false;
searching = false;
while (find()) {
appendReplacement(buffer, replacement);
}
return appendTail(buffer).toString();
|
public java.lang.String | replaceFirst(java.lang.String replacement)Replaces the first occurrence of this matcher's pattern in the input with
a given string.
StringBuffer buffer = new StringBuffer(input.length());
findPos = 0;
appendPos = 0;
matchFound = false;
searching = false;
if (find()) {
appendReplacement(buffer, replacement);
}
return appendTail(buffer).toString();
|
public boolean | requireEnd()Indicates whether more input might change a successful match into an
unsuccessful one.
return NativeRegEx.requireEnd(nativePattern);
|
private java.util.regex.Matcher | reset(java.lang.CharSequence input, int start, int end)Resets the Matcher. A new input sequence and a new region can be
specified. Results of a previous find get lost. The next attempt to find
an occurrence of the Pattern in the string will start at the beginning of
the region. This is the internal version of reset() to which the several
public versions delegate.
if (input == null) {
throw new IllegalArgumentException();
}
if (start < 0 || end < 0 || start > input.length() ||
end > input.length() || start > end) {
throw new IllegalArgumentException();
}
// Maybe should have a reset() here, but it makes thing worse...
// NativeRegEx.reset(nativePattern, 0);
if (!input.equals(this.input)) {
this.input = input.toString();
NativeRegEx.setText(nativePattern, this.input);
regionStart = 0;
regionEnd = input.length();
}
if (start != regionStart || end != regionEnd) {
regionStart = start;
regionEnd = end;
NativeRegEx.setRegion(nativePattern, regionStart, regionEnd);
}
searching = false;
matchFound = false;
findPos = regionStart;
appendPos = 0;
return this;
|
public java.util.regex.Matcher | reset()Resets the {@code Matcher}. This results in the region being set to the
whole input. Results of a previous find get lost. The next attempt to
find an occurrence of the {@link Pattern} in the string will start at the
beginning of the input.
return reset(input, 0, input.length());
|
public java.util.regex.Matcher | reset(java.lang.CharSequence input)Provides a new input and resets the {@code Matcher}. This results in the
region being set to the whole input. Results of a previous find get lost.
The next attempt to find an occurrence of the {@link Pattern} in the
string will start at the beginning of the input.
return reset(input, 0, input.length());
|
public int | start()Returns the index of the first character of the text that matched the
whole regular expression.
return start(0);
|
public int | start(int group)Returns the index of the first character of the text that matched a given
group.
ensureMatch();
return matchOffsets[group * 2];
|
public java.util.regex.MatchResult | toMatchResult()Converts the current match into a separate {@link MatchResult} instance
that is independent from this matcher. The new object is unaffected when
the state of this matcher changes.
ensureMatch();
return new MatchResultImpl(input, matchOffsets);
|
public java.util.regex.Matcher | useAnchoringBounds(boolean value)Determines whether this matcher has anchoring bounds enabled or not. When
anchoring bounds are enabled, the start and end of the input match the
'^' and '$' meta-characters, otherwise not. Anchoring bounds are enabled
by default.
anchoringBounds = value;
NativeRegEx.useAnchoringBounds(nativePattern, value);
return this;
|
public java.util.regex.Matcher | usePattern(java.util.regex.Pattern pattern)Sets a new pattern for the {@code Matcher}. Results of a previous find
get lost. The next attempt to find an occurrence of the {@link Pattern}
in the string will start at the beginning of the input.
if (pattern == null) {
throw new IllegalArgumentException();
}
this.pattern = pattern;
if (nativePattern != 0) {
NativeRegEx.close(nativePattern);
}
nativePattern = NativeRegEx.clone(pattern.mNativePattern);
if (input != null) {
NativeRegEx.setText(nativePattern, input);
NativeRegEx.setRegion(nativePattern, regionStart, regionEnd);
NativeRegEx.useAnchoringBounds(nativePattern, anchoringBounds);
NativeRegEx.useTransparentBounds(nativePattern, transparentBounds);
}
matchOffsets = new int[(this.pattern.mGroupCount + 1) * 2];
matchFound = false;
return this;
|
public java.util.regex.Matcher | useTransparentBounds(boolean value)Determines whether this matcher has transparent bounds enabled or not.
When transparent bounds are enabled, the parts of the input outside the
region are subject to lookahead and lookbehind, otherwise they are not.
Transparent bounds are disabled by default.
transparentBounds = value;
NativeRegEx.useTransparentBounds(nativePattern, value);
return this;
|