Skip to content

daft.functions.regexp_extract_all#

regexp_extract_all #

regexp_extract_all(expr: Expression, pattern: str | Expression, index: int = 0) -> Expression

Extracts the specified match group from all regex matches in each string in a string column.

Parameters:

Name Type Description Default
expr Expression

String expression to extract from

required
pattern str | Expression

The regex pattern to extract

required
index int

The index of the regex match group to extract

0

Returns:

Name Type Description
Expression Expression

a List[String] expression with the extracted regex matches

Note

This expression always returns a list of strings. If index is 0, the entire match is returned. If the pattern does not match or the group does not exist, an empty list is returned.

Examples:

1
2
3
4
5
6
>>> import daft
>>> from daft.functions import regexp_extract_all
>>>
>>> regex = r"(\d)(\d*)"
>>> df = daft.from_pydict({"x": ["123-456", "789-012", "345-678"]})
>>> df.with_column("match", regexp_extract_all(df["x"], regex)).collect()
╭─────────┬──────────────╮
│ x       ┆ match        │
│ ---     ┆ ---          │
│ String  ┆ List[String] │
╞═════════╪══════════════╡
│ 123-456 ┆ [123, 456]   │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 789-012 ┆ [789, 012]   │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 345-678 ┆ [345, 678]   │
╰─────────┴──────────────╯
(Showing first 3 of 3 rows)

Extract the first capture group

1
>>> df.with_column("match", regexp_extract_all(df["x"], regex, 1)).collect()
╭─────────┬──────────────╮
│ x       ┆ match        │
│ ---     ┆ ---          │
│ String  ┆ List[String] │
╞═════════╪══════════════╡
│ 123-456 ┆ [1, 4]       │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 789-012 ┆ [7, 0]       │
├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 345-678 ┆ [3, 6]       │
╰─────────┴──────────────╯
(Showing first 3 of 3 rows)
See Also

regexp_extract

Source code in daft/functions/str.py
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
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
def regexp_extract_all(expr: Expression, pattern: str | Expression, index: int = 0) -> Expression:
    r"""Extracts the specified match group from all regex matches in each string in a string column.

    Args:
        expr: String expression to extract from
        pattern: The regex pattern to extract
        index: The index of the regex match group to extract

    Returns:
        Expression: a List[String] expression with the extracted regex matches

    Note:
        This expression always returns a list of strings.
        If index is 0, the entire match is returned. If the pattern does not match or the group does not exist, an empty list is returned.

    Examples:
        >>> import daft
        >>> from daft.functions import regexp_extract_all
        >>>
        >>> regex = r"(\d)(\d*)"
        >>> df = daft.from_pydict({"x": ["123-456", "789-012", "345-678"]})
        >>> df.with_column("match", regexp_extract_all(df["x"], regex)).collect()
        ╭─────────┬──────────────╮
        │ x       ┆ match        │
        │ ---     ┆ ---          │
        │ String  ┆ List[String] │
        ╞═════════╪══════════════╡
        │ 123-456 ┆ [123, 456]   │
        ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 789-012 ┆ [789, 012]   │
        ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 345-678 ┆ [345, 678]   │
        ╰─────────┴──────────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

        Extract the first capture group

        >>> df.with_column("match", regexp_extract_all(df["x"], regex, 1)).collect()
        ╭─────────┬──────────────╮
        │ x       ┆ match        │
        │ ---     ┆ ---          │
        │ String  ┆ List[String] │
        ╞═════════╪══════════════╡
        │ 123-456 ┆ [1, 4]       │
        ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 789-012 ┆ [7, 0]       │
        ├╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ 345-678 ┆ [3, 6]       │
        ╰─────────┴──────────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    Tip: See Also
        [`regexp_extract`](https://docs.daft.ai/en/stable/api/functions/regexp_extract/)
    """
    return Expression._call_builtin_scalar_fn("regexp_extract_all", expr, pattern, index)