Share via


Matcher.ReplaceFirst Method

Definition

Overloads

ReplaceFirst(IFunction)

Replaces the first subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence.

ReplaceFirst(String)

Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.

ReplaceFirst(IFunction)

Replaces the first subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence.

[Android.Runtime.Register("replaceFirst", "(Ljava/util/function/Function;)Ljava/lang/String;", "", ApiSince=34)]
public string ReplaceFirst (Java.Util.Functions.IFunction replacer);
[<Android.Runtime.Register("replaceFirst", "(Ljava/util/function/Function;)Ljava/lang/String;", "", ApiSince=34)>]
member this.ReplaceFirst : Java.Util.Functions.IFunction -> string

Parameters

replacer
IFunction

The function to be applied to the match result of this matcher that returns a replacement string.

Returns

The string constructed by replacing the first matching subsequence with the result of applying the replacer function to the matched subsequence, substituting captured subsequences as needed.

Attributes

Remarks

Replaces the first subsequence of the input sequence that matches the pattern with the result of applying the given replacer function to the match result of this matcher corresponding to that subsequence. Exceptions thrown by the replace function are relayed to the caller.

This method first resets this matcher. It then scans the input sequence looking for a match of the pattern. Characters that are not part of the match are appended directly to the result string; the match is replaced in the result by the applying the replacer function that returns a replacement string. The replacement string may contain references to captured subsequences as in the #appendReplacement appendReplacement method.

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.

Given the regular expression dog, the input "zzzdogzzzdogzzz", and the function mr -> mr.group().toUpperCase(), an invocation of this method on a matcher for that expression would yield the string "zzzDOGzzzdogzzz".

Invoking this method changes this matcher's state. If the matcher is to be used in further matching operations then it should first be reset.

The replacer function should not modify this matcher's state during replacement. This method will, on a best-effort basis, throw a java.util.ConcurrentModificationException if such modification is detected.

The state of the match result passed to the replacer function is guaranteed to be constant only for the duration of the replacer function call and only if the replacer function does not modify this matcher's state.

Added in 9.

Java documentation for java.util.regex.Matcher.replaceFirst(java.util.function.Function<java.util.regex.MatchResult, 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

ReplaceFirst(String)

Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.

[Android.Runtime.Register("replaceFirst", "(Ljava/lang/String;)Ljava/lang/String;", "")]
public string ReplaceFirst (string replacement);
[<Android.Runtime.Register("replaceFirst", "(Ljava/lang/String;)Ljava/lang/String;", "")>]
member this.ReplaceFirst : string -> string

Parameters

replacement
String

The replacement string

Returns

The string constructed by replacing the first matching subsequence by the replacement string, substituting captured subsequences as needed

Attributes

Remarks

Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.

This method first resets this matcher. It then scans the input sequence looking for a match of the pattern. Characters that are not part of the match are appended directly to the result string; the match is replaced in the result by the replacement string. The replacement string may contain references to captured subsequences as in the #appendReplacement appendReplacement method.

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.

Given the regular expression dog, the input "zzzdogzzzdogzzz", and the replacement string "cat", an invocation of this method on a matcher for that expression would yield the string "zzzcatzzzdogzzz".

Invoking this method changes this matcher's state. If the matcher is to be used in further matching operations then it should first be reset.

Java documentation for java.util.regex.Matcher.replaceFirst(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