polars.Expr.str.replace_all#

Expr.str.replace_all(
pattern: str | Expr,
value: str | Expr,
*,
literal: bool = False,
) Expr[source]#

Replace all matching regex/literal substrings with a new string value.

Parameters:
pattern

A valid regular expression pattern, compatible with the regex crate.

value

Replacement string.

literal

Treat pattern as a literal string.

See also

replace

Replace first matching regex/literal substring.

Examples

>>> df = pl.DataFrame({"id": [1, 2], "text": ["abcabc", "123a123"]})
>>> df.with_columns(pl.col("text").str.replace_all("a", "-"))
shape: (2, 2)
┌─────┬─────────┐
│ id  ┆ text    │
│ --- ┆ ---     │
│ i64 ┆ str     │
╞═════╪═════════╡
│ 1   ┆ -bc-bc  │
│ 2   ┆ 123-123 │
└─────┴─────────┘