replace

expect fun replace(input: CharSequence, replacement: String): String(source)
expect fun replace(input: CharSequence, transform: (MatchResult) -> CharSequence): String(source)

Since Kotlin

1.0
actual fun replace(input: CharSequence, replacement: String): String(source)

Replaces all occurrences of this regular expression in the specified input string with specified replacement expression.

The replacement string may contain references to the captured groups during a match. Occurrences of ${name} or $index in the replacement string will be substituted with the subsequences corresponding to the captured groups with the specified name or index. In case of $index, the first digit after '$' is always treated as a part of group reference. Subsequent digits are incorporated into index only if they would form a valid group reference. Only the digits '0'..'9' are considered as potential components of the group reference. Note that indexes of captured groups start from 1, and the group with index 0 is the whole match. In case of ${name}, the name can consist of latin letters 'a'..'z' and 'A'..'Z', or digits '0'..'9'. The first character must be a letter.

Backslash character '\' can be used to include the succeeding character as a literal in the replacement string, e.g, \$ or \\. Regex.escapeReplacement can be used if replacement have to be treated as a literal string.

Since Kotlin

1.1

Return

the result of replacing each occurrence of this regular expression in input with the result of evaluating the replacement expression

Parameters

input

the char sequence to find matches of this regular expression in

replacement

the expression to replace found matches with

Throws

if replacement expression is malformed, or capturing group with specified name or index does not exist


actual fun replace(input: CharSequence, transform: (MatchResult) -> CharSequence): String(source)

Replaces all occurrences of this regular expression in the specified input string with the result of the given function transform that takes MatchResult and returns a string to be used as a replacement for that match.

Since Kotlin

1.1
actual fun replace(input: CharSequence, replacement: String): String(source)

Replaces all occurrences of this regular expression in the specified input string with specified replacement expression.

The replacement string may contain references to the captured groups during a match. Occurrences of ${name} or $index in the replacement string will be substituted with the subsequences corresponding to the captured groups with the specified name or index. In case of $index, the first digit after '$' is always treated as a part of group reference. Subsequent digits are incorporated into index only if they would form a valid group reference. Only the digits '0'..'9' are considered as potential components of the group reference. Note that indexes of captured groups start from 1, and the group with index 0 is the whole match. In case of ${name}, the name can consist of latin letters 'a'..'z' and 'A'..'Z', or digits '0'..'9'. The first character must be a letter.

Backslash character '\' can be used to include the succeeding character as a literal in the replacement string, e.g, \$ or \\. Regex.escapeReplacement can be used if replacement have to be treated as a literal string.

Note that named capturing groups are supported in Java 7 or later.

Since Kotlin

1.0

Return

the result of replacing each occurrence of this regular expression in input with the result of evaluating the replacement expression

Parameters

input

the char sequence to find matches of this regular expression in

replacement

the expression to replace found matches with

Throws

if replacement expression is malformed, or capturing group with specified name or index does not exist


actual fun replace(input: CharSequence, transform: (MatchResult) -> CharSequence): String(source)

Replaces all occurrences of this regular expression in the specified input string with the result of the given function transform that takes MatchResult and returns a string to be used as a replacement for that match.

Since Kotlin

1.0
actual fun replace(input: CharSequence, replacement: String): String(source)

Replaces all occurrences of this regular expression in the specified input string with specified replacement expression.

The replacement string may contain references to the captured groups during a match. Occurrences of ${name} or $index in the replacement string will be substituted with the subsequences corresponding to the captured groups with the specified name or index. In case of $index, the first digit after '$' is always treated as a part of group reference. Subsequent digits are incorporated into index only if they would form a valid group reference. Only the digits '0'..'9' are considered as potential components of the group reference. Note that indexes of captured groups start from 1, and the group with index 0 is the whole match. In case of ${name}, the name can consist of latin letters 'a'..'z' and 'A'..'Z', or digits '0'..'9'. The first character must be a letter.

Backslash character '\' can be used to include the succeeding character as a literal in the replacement string, e.g, \$ or \\. Regex.escapeReplacement can be used if replacement have to be treated as a literal string.

Since Kotlin

1.3

Return

the result of replacing each occurrence of this regular expression in input with the result of evaluating the replacement expression

Parameters

input

the char sequence to find matches of this regular expression in

replacement

the expression to replace found matches with

Throws

if replacement expression is malformed, or capturing group with specified name or index does not exist


actual fun replace(input: CharSequence, transform: (MatchResult) -> CharSequence): String(source)

Replaces all occurrences of this regular expression in the specified input string with the result of the given function transform that takes MatchResult and returns a string to be used as a replacement for that match.

Since Kotlin

1.3