replace
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.0Return
the result of replacing each occurrence of this regular expression in input with the result of evaluating the replacement expression
Parameters
the char sequence to find matches of this regular expression in
the expression to replace found matches with
Throws
if replacement expression is malformed, or capturing group with specified name
or index
does not exist
Samples
fun main() {
//sampleStart
val text = "The events are on 15-09-2024 and 16-10-2024."
// Regex to match dates in dd-mm-yyyy format
val dateRegex = "(?<day>\\d{2})-(?<month>\\d{2})-(?<year>\\d{4})".toRegex()
// Replacement expression that puts day, month and year values in the ISO 8601 recommended yyyy-mm-dd format
val replacement = "\${year}-\${month}-\${day}"
// Replacing all occurrences of dates from dd-mm-yyyy to yyyy-mm-dd format
println(dateRegex.replace(text, replacement)) // The events are on 2024-09-15 and 2024-10-16.
// One can also reference the matched groups by index
println(dateRegex.replace(text, "$3-$2-$1")) // The events are on 2024-09-15 and 2024-10-16.
// Using a backslash to include the special character '$' as a literal in the result
println(dateRegex.replace(text, "$3-\\$2-$1")) // The events are on 2024-\$2-15 and 2024-\$2-16.
//sampleEnd
}
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.0Replaces 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.1Return
the result of replacing each occurrence of this regular expression in input with the result of evaluating the replacement expression
Parameters
the char sequence to find matches of this regular expression in
the expression to replace found matches with
Throws
if replacement expression is malformed, or capturing group with specified name
or index
does not exist
Samples
fun main() {
//sampleStart
val text = "The events are on 15-09-2024 and 16-10-2024."
// Regex to match dates in dd-mm-yyyy format
val dateRegex = "(?<day>\\d{2})-(?<month>\\d{2})-(?<year>\\d{4})".toRegex()
// Replacement expression that puts day, month and year values in the ISO 8601 recommended yyyy-mm-dd format
val replacement = "\${year}-\${month}-\${day}"
// Replacing all occurrences of dates from dd-mm-yyyy to yyyy-mm-dd format
println(dateRegex.replace(text, replacement)) // The events are on 2024-09-15 and 2024-10-16.
// One can also reference the matched groups by index
println(dateRegex.replace(text, "$3-$2-$1")) // The events are on 2024-09-15 and 2024-10-16.
// Using a backslash to include the special character '$' as a literal in the result
println(dateRegex.replace(text, "$3-\\$2-$1")) // The events are on 2024-\$2-15 and 2024-\$2-16.
//sampleEnd
}
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.1Replaces 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.0Return
the result of replacing each occurrence of this regular expression in input with the result of evaluating the replacement expression
Parameters
the char sequence to find matches of this regular expression in
the expression to replace found matches with
Throws
if replacement expression is malformed, or capturing group with specified name
or index
does not exist
Samples
fun main() {
//sampleStart
val text = "The events are on 15-09-2024 and 16-10-2024."
// Regex to match dates in dd-mm-yyyy format
val dateRegex = "(?<day>\\d{2})-(?<month>\\d{2})-(?<year>\\d{4})".toRegex()
// Replacement expression that puts day, month and year values in the ISO 8601 recommended yyyy-mm-dd format
val replacement = "\${year}-\${month}-\${day}"
// Replacing all occurrences of dates from dd-mm-yyyy to yyyy-mm-dd format
println(dateRegex.replace(text, replacement)) // The events are on 2024-09-15 and 2024-10-16.
// One can also reference the matched groups by index
println(dateRegex.replace(text, "$3-$2-$1")) // The events are on 2024-09-15 and 2024-10-16.
// Using a backslash to include the special character '$' as a literal in the result
println(dateRegex.replace(text, "$3-\\$2-$1")) // The events are on 2024-\$2-15 and 2024-\$2-16.
//sampleEnd
}
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.0Replaces 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.3Return
the result of replacing each occurrence of this regular expression in input with the result of evaluating the replacement expression
Parameters
the char sequence to find matches of this regular expression in
the expression to replace found matches with
Throws
if replacement expression is malformed, or capturing group with specified name
or index
does not exist
Samples
fun main() {
//sampleStart
val text = "The events are on 15-09-2024 and 16-10-2024."
// Regex to match dates in dd-mm-yyyy format
val dateRegex = "(?<day>\\d{2})-(?<month>\\d{2})-(?<year>\\d{4})".toRegex()
// Replacement expression that puts day, month and year values in the ISO 8601 recommended yyyy-mm-dd format
val replacement = "\${year}-\${month}-\${day}"
// Replacing all occurrences of dates from dd-mm-yyyy to yyyy-mm-dd format
println(dateRegex.replace(text, replacement)) // The events are on 2024-09-15 and 2024-10-16.
// One can also reference the matched groups by index
println(dateRegex.replace(text, "$3-$2-$1")) // The events are on 2024-09-15 and 2024-10-16.
// Using a backslash to include the special character '$' as a literal in the result
println(dateRegex.replace(text, "$3-\\$2-$1")) // The events are on 2024-\$2-15 and 2024-\$2-16.
//sampleEnd
}
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.3Replaces 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.8Return
the result of replacing each occurrence of this regular expression in input with the result of evaluating the replacement expression
Parameters
the char sequence to find matches of this regular expression in
the expression to replace found matches with
Throws
if replacement expression is malformed, or capturing group with specified name
or index
does not exist
Samples
fun main() {
//sampleStart
val text = "The events are on 15-09-2024 and 16-10-2024."
// Regex to match dates in dd-mm-yyyy format
val dateRegex = "(?<day>\\d{2})-(?<month>\\d{2})-(?<year>\\d{4})".toRegex()
// Replacement expression that puts day, month and year values in the ISO 8601 recommended yyyy-mm-dd format
val replacement = "\${year}-\${month}-\${day}"
// Replacing all occurrences of dates from dd-mm-yyyy to yyyy-mm-dd format
println(dateRegex.replace(text, replacement)) // The events are on 2024-09-15 and 2024-10-16.
// One can also reference the matched groups by index
println(dateRegex.replace(text, "$3-$2-$1")) // The events are on 2024-09-15 and 2024-10-16.
// Using a backslash to include the special character '$' as a literal in the result
println(dateRegex.replace(text, "$3-\\$2-$1")) // The events are on 2024-\$2-15 and 2024-\$2-16.
//sampleEnd
}
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.8Replaces 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.8Return
the result of replacing each occurrence of this regular expression in input with the result of evaluating the replacement expression
Parameters
the char sequence to find matches of this regular expression in
the expression to replace found matches with
Throws
if replacement expression is malformed, or capturing group with specified name
or index
does not exist
Samples
fun main() {
//sampleStart
val text = "The events are on 15-09-2024 and 16-10-2024."
// Regex to match dates in dd-mm-yyyy format
val dateRegex = "(?<day>\\d{2})-(?<month>\\d{2})-(?<year>\\d{4})".toRegex()
// Replacement expression that puts day, month and year values in the ISO 8601 recommended yyyy-mm-dd format
val replacement = "\${year}-\${month}-\${day}"
// Replacing all occurrences of dates from dd-mm-yyyy to yyyy-mm-dd format
println(dateRegex.replace(text, replacement)) // The events are on 2024-09-15 and 2024-10-16.
// One can also reference the matched groups by index
println(dateRegex.replace(text, "$3-$2-$1")) // The events are on 2024-09-15 and 2024-10-16.
// Using a backslash to include the special character '$' as a literal in the result
println(dateRegex.replace(text, "$3-\\$2-$1")) // The events are on 2024-\$2-15 and 2024-\$2-16.
//sampleEnd
}
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.