Share via


Matcher.AppendReplacement Method

Definition

Overloads

AppendReplacement(StringBuffer, String)

Implements a non-terminal append-and-replace step.

AppendReplacement(StringBuilder, String)

Implements a non-terminal append-and-replace step.

AppendReplacement(StringBuffer, String)

Implements a non-terminal append-and-replace step.

[Android.Runtime.Register("appendReplacement", "(Ljava/lang/StringBuffer;Ljava/lang/String;)Ljava/util/regex/Matcher;", "")]
public Java.Util.Regex.Matcher AppendReplacement (Java.Lang.StringBuffer sb, string replacement);
[<Android.Runtime.Register("appendReplacement", "(Ljava/lang/StringBuffer;Ljava/lang/String;)Ljava/util/regex/Matcher;", "")>]
member this.AppendReplacement : Java.Lang.StringBuffer * string -> Java.Util.Regex.Matcher

Parameters

sb
StringBuffer

The target string buffer

replacement
String

The replacement string

Returns

This matcher

Attributes

Exceptions

if no successful match has been made.

Remarks

Implements a non-terminal append-and-replace step.

This method performs the following actions:

<ol>

<li>

It reads characters from the input sequence, starting at the append position, and appends them to the given string buffer. It stops after reading the last character preceding the previous match, that is, the character at index #start()&nbsp;-&nbsp;1.

</li>

<li>

It appends the given replacement string to the string buffer.

</li>

<li>

It sets the append position of this matcher to the index of the last character matched, plus one, that is, to #end().

</li>

</ol>

The replacement string may contain references to subsequences captured during the previous match: Each occurrence of ${name} or $g will be replaced by the result of evaluating the corresponding #group(String) group(name) or #group(int) group(g) respectively. For $g, the first number after the $ is always treated as part of the group reference. Subsequent numbers are incorporated into g if they would form a legal group reference. Only the numerals '0' through '9' are considered as potential components of the group reference. If the second group matched the string "foo", for example, then passing the replacement string "$2bar" would cause "foobar" to be appended to the string buffer. A dollar sign ($) may be included as a literal in the replacement string by preceding it with a backslash (\$).

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

This method is intended to be used in a loop together with the #appendTail(StringBuffer) appendTail and #find() find methods. The following code, for example, writes one dog two dogs in the yard to the standard-output stream:

<blockquote>

Pattern p = Pattern.compile("cat");
            Matcher m = p.matcher("one cat two cats in the yard");
            StringBuffer sb = new StringBuffer();
            while (m.find()) {
                m.appendReplacement(sb, "dog");
            }
            m.appendTail(sb);
            System.out.println(sb.toString());

</blockquote>

Java documentation for java.util.regex.Matcher.appendReplacement(java.lang.StringBuffer, java.lang.String).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

AppendReplacement(StringBuilder, String)

Implements a non-terminal append-and-replace step.

[Android.Runtime.Register("appendReplacement", "(Ljava/lang/StringBuilder;Ljava/lang/String;)Ljava/util/regex/Matcher;", "", ApiSince=34)]
public Java.Util.Regex.Matcher AppendReplacement (Java.Lang.StringBuilder sb, string replacement);
[<Android.Runtime.Register("appendReplacement", "(Ljava/lang/StringBuilder;Ljava/lang/String;)Ljava/util/regex/Matcher;", "", ApiSince=34)>]
member this.AppendReplacement : Java.Lang.StringBuilder * string -> Java.Util.Regex.Matcher

Parameters

sb
StringBuilder

The target string builder

replacement
String

The replacement string

Returns

This matcher

Attributes

Remarks

Implements a non-terminal append-and-replace step.

This method performs the following actions:

<ol>

<li>

It reads characters from the input sequence, starting at the append position, and appends them to the given string builder. It stops after reading the last character preceding the previous match, that is, the character at index #start()&nbsp;-&nbsp;1.

</li>

<li>

It appends the given replacement string to the string builder.

</li>

<li>

It sets the append position of this matcher to the index of the last character matched, plus one, that is, to #end().

</li>

</ol>

The replacement string may contain references to subsequences captured during the previous match: Each occurrence of $g will be replaced by the result of evaluating #group(int) group(g). The first number after the $ is always treated as part of the group reference. Subsequent numbers are incorporated into g if they would form a legal group reference. Only the numerals '0' through '9' are considered as potential components of the group reference. If the second group matched the string "foo", for example, then passing the replacement string "$2bar" would cause "foobar" to be appended to the string builder. A dollar sign ($) may be included as a literal in the replacement string by preceding it with a backslash (\$).

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

This method is intended to be used in a loop together with the #appendTail(StringBuilder) appendTail and #find() find methods. The following code, for example, writes one dog two dogs in the yard to the standard-output stream:

<blockquote>

Pattern p = Pattern.compile("cat");
            Matcher m = p.matcher("one cat two cats in the yard");
            StringBuilder sb = new StringBuilder();
            while (m.find()) {
                m.appendReplacement(sb, "dog");
            }
            m.appendTail(sb);
            System.out.println(sb.toString());

</blockquote>

Added in 9.

Java documentation for java.util.regex.Matcher.appendReplacement(java.lang.StringBuilder, java.lang.String).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to