Skip to content

daft.functions.regexp_replace#

regexp_replace #

regexp_replace(expr: Expression, pattern: str | Expression, replacement: str | Expression) -> Expression

Replaces all occurrences of a regex pattern in a string column with a replacement string.

Parameters:

Name Type Description Default
expr Expression

The string expression to be replaced

required
pattern str | Expression

The pattern to replace

required
replacement str | Expression

The replacement string

required

Returns:

Name Type Description
Expression Expression

a String expression with patterns replaced by the replacement string

Examples:

1
2
3
4
5
>>> import daft
>>> from daft.functions import regexp_replace
>>>
>>> df = daft.from_pydict({"data": ["foo", "fooo", "foooo"]})
>>> df.with_column("replace", regexp_replace(df["data"], r"o+", "a")).collect()
╭────────┬─────────╮
│ data   ┆ replace │
│ ---    ┆ ---     │
│ String ┆ String  │
╞════════╪═════════╡
│ foo    ┆ fa      │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ fooo   ┆ fa      │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ foooo  ┆ fa      │
╰────────┴─────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/str.py
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
def regexp_replace(
    expr: Expression,
    pattern: str | Expression,
    replacement: str | Expression,
) -> Expression:
    """Replaces all occurrences of a regex pattern in a string column with a replacement string.

    Args:
        expr: The string expression to be replaced
        pattern: The pattern to replace
        replacement: The replacement string

    Returns:
        Expression: a String expression with patterns replaced by the replacement string

    Examples:
        >>> import daft
        >>> from daft.functions import regexp_replace
        >>>
        >>> df = daft.from_pydict({"data": ["foo", "fooo", "foooo"]})
        >>> df.with_column("replace", regexp_replace(df["data"], r"o+", "a")).collect()
        ╭────────┬─────────╮
        │ data   ┆ replace │
        │ ---    ┆ ---     │
        │ String ┆ String  │
        ╞════════╪═════════╡
        │ foo    ┆ fa      │
        ├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
        │ fooo   ┆ fa      │
        ├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
        │ foooo  ┆ fa      │
        ╰────────┴─────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    return Expression._call_builtin_scalar_fn("regexp_replace", expr, pattern, replacement)