Function: text.replace_all_regex

Replaces all matches of a regular expression with a new string.

In the case of overlapping matches, the leftmost instance is replaced and ifmultiple matches start at the same index, then the longest one is replaced.If the expression is empty, it matches at the beginning of the string andafter each code point, yielding up tok+1 replacements for a string withk code points.

Note that regular expression backreferences are not supported and you can'trefer to text previously matched by a capturing group.

Arguments

Arguments
source

string

The subject of the function. A modified copy is returned.

regexp

string

The regular expression used for matching substrings. UsesRE2 syntax.

repl

string

The string that will replace all matches ofregexp.

Backreferences are not supported. For example, you can't use the expression\1 to refer to text previously matched by a capturing group.

Returns

A copy ofsource where all matches ofregexp are replaced byrepl.

Raised exceptions

Exceptions
TypeErrorIf eithersource,regexp, orrepl is not a string.
ValueErrorIf eitherregexp orrepl is not UTF-8 encoded or ifregexp is an invalid regular expression.

Examples

# Replace matches of regular expression; returns "three three"-returnStep:return:${text.replace_all_regex("one two", "one|two", "three")}

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.