| Method from java.util.regex.Matcher Detail: |
public Matcher appendReplacement(StringBuffer buffer,
String replacement) {
processedRepl = processReplacement(replacement);
buffer.append(string.subSequence(appendPos, start()));
buffer.append(processedRepl);
appendPos = end();
return this;
}
Appends a literal part of the input plus a replacement for the current
match to a given 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 #find() and
#appendTail(StringBuffer) to walk through the input and replace
all occurrences of the {@code Pattern} with something else. |
public StringBuffer appendTail(StringBuffer buffer) {
return buffer.append(string.subSequence(appendPos, string.length()));
}
|
public int end() {
return end(0);
}
Returns the index of the first character following the text that matched
the whole regular expression. |
public int end(int group) {
return matchResult.end(group);
}
Returns the index of the first character following the text that matched
a given group. |
public boolean find() {
int length = string.length();
if (!hasTransparentBounds())
length = rightBound;
if (matchResult.startIndex >= 0
&& matchResult.mode() == Matcher.MODE_FIND) {
matchResult.startIndex = matchResult.end();
if (matchResult.end() == matchResult.start()) {
matchResult.startIndex++;
}
return matchResult.startIndex < = length ? find(matchResult.startIndex)
: false;
} else {
return find(leftBound);
}
}
Returns the next occurrence of the 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. |
public boolean find(int start) {
int stringLength = string.length();
if (start < 0 || start > stringLength) {
throw new IndexOutOfBoundsException(Messages.getString("regex.03", //$NON-NLS-1$
Integer.valueOf(start)));
}
start = findAt(start);
if (start >= 0 && matchResult.isValid()) {
matchResult.finalizeMatch();
return true;
}
matchResult.startIndex = -1;
return false;
}
Returns the next occurrence of the Pattern in the input. The
method starts the search from the given character in the input. |
public String group() {
return group(0);
}
Returns the text that matched the whole regular expression. |
public String group(int group) {
return matchResult.group(group);
}
Returns the text that matched a given group of the regular expression. |
public int groupCount() {
return matchResult.groupCount();
}
Returns the number of groups in the results, which is always equal to
the number of groups in the original regular expression. |
public boolean hasAnchoringBounds() {
return matchResult.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. |
public boolean hasTransparentBounds() {
return matchResult.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. |
public boolean hitEnd() {
return matchResult.hitEnd;
}
Indicates whether the last match hit the end of the input. |
public boolean lookingAt() {
return lookingAt(leftBound, Matcher.MODE_FIND);
}
Tries to match the 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. |
public boolean matches() {
return lookingAt(leftBound, Matcher.MODE_MATCH);
}
Tries to match the Pattern against the entire region (or the
entire input, if no region has been set). |
public Pattern pattern() {
return pat;
}
Returns the Pattern instance used inside this matcher. |
public static String quoteReplacement(String s) {
// first check whether we have smth to quote
if (s.indexOf('\\') < 0 && s.indexOf('$') < 0)
return s;
StringBuilder res = new StringBuilder(s.length() * 2);
char ch;
int len = s.length();
for (int i = 0; i < len; i++) {
switch (ch = s.charAt(i)) {
case '$':
res.append('\\');
res.append('$');
break;
case '\\':
res.append('\\');
res.append('\\');
break;
default:
res.append(ch);
}
}
return res.toString();
}
Returns a replacement string for the given one that has all backslashes
and dollar signs escaped. |
public Matcher region(int start,
int end) {
if (start > end || start < 0 || end < 0
|| start > string.length() || end > string.length()) {
throw new IndexOutOfBoundsException( Messages.getString("regex.02", //$NON-NLS-1$
Integer.toString(start), Integer.toString(end)));
}
this.leftBound = start;
this.rightBound = end;
matchResult.reset(null, start, end);
appendPos = 0;
replacement = null;
return this;
}
Resets this matcher and sets a region. Only characters inside the region
are considered for a match. |
public int regionEnd() {
return matchResult.getRightBound();
}
Returns this matcher's region end, that is, the first character that is
not considered for a match. |
public int regionStart() {
return matchResult.getLeftBound();
}
Returns this matcher's region start, that is, the first character that is
considered for a match. |
public String replaceAll(String replacement) {
StringBuffer sb = new StringBuffer();
reset();
while (find()) {
appendReplacement(sb, replacement);
}
return appendTail(sb).toString();
}
Replaces all occurrences of this matcher's pattern in the input with a
given string. |
public String replaceFirst(String replacement) {
reset();
if (find()) {
StringBuffer sb = new StringBuffer();
appendReplacement(sb, replacement);
return appendTail(sb).toString();
}
return string.toString();
}
Replaces the first occurrence of this matcher's pattern in the input with
a given string. |
public boolean requireEnd() {
return matchResult.requireEnd;
}
Indicates whether more input might change a successful match into an
unsuccessful one. |
public Matcher reset() {
this.leftBound = 0;
this.rightBound = string.length();
matchResult.reset(string, leftBound, rightBound);
appendPos = 0;
replacement = null;
matchResult.previousMatch = -1;
return this;
}
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 Pattern in the string will start at the
beginning of the input. |
public Matcher reset(CharSequence input) {
if (input == null) {
throw new NullPointerException(Messages.getString("regex.01")); //$NON-NLS-1$
}
this.string = input;
return reset();
}
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 Pattern in the
string will start at the beginning of the input. |
public int start() {
return start(0);
}
Returns the index of the first character of the text that matched the
whole regular expression. |
public int start(int group) {
return matchResult.start(group);
}
Returns the index of the first character of the text that matched a given
group. |
public MatchResult toMatchResult() {
return this.matchResult.cloneImpl();
}
Converts the current match into a separate MatchResult instance
that is independent from this matcher. The new object is unaffected when
the state of this matcher changes. |
public Matcher useAnchoringBounds(boolean value) {
matchResult.useAnchoringBounds(value);
return this;
}
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. |
public Matcher usePattern(Pattern pattern) {
if (pattern == null) {
throw new IllegalArgumentException(Messages.getString("regex.1B"));
}
int startIndex = matchResult.getPreviousMatchEnd();
int mode = matchResult.mode();
this.pat = pattern;
this.start = pattern.start;
matchResult = new MatchResultImpl(this.string, leftBound, rightBound,
pattern.groupCount(), pattern.compCount(), pattern.consCount());
matchResult.setStartIndex(startIndex);
matchResult.setMode(mode);
return this;
}
Sets a new pattern for the {@code Matcher}. 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 input. |
public Matcher useTransparentBounds(boolean value) {
matchResult.useTransparentBounds(value);
return this;
}
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. |