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
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
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)