Skip to content

daft.functions.regexp_count#

regexp_count #

regexp_count(expr: Expression, pattern: str | Expression) -> Expression

Counts the number of times a regex pattern appears in a string.

Parameters:

Name Type Description Default
expr Expression

The expression to check.

required
pattern str | Expression

The regex pattern to search for as a string or as a column to pick values from.

required

Returns:

Name Type Description
Expression Expression

An UInt64 expression with the count of regex matches for each string.

Examples:

1
2
3
4
>>> import daft
>>> from daft.functions import regexp_count
>>> df = daft.from_pydict({"x": ["hello world", "foo bar baz", "test123test456"]})
>>> df.with_column("word_count", regexp_count(df["x"], r"\w+")).collect()
╭────────────────┬────────────╮
│ x              ┆ word_count │
│ ---            ┆ ---        │
│ String         ┆ UInt64     │
╞════════════════╪════════════╡
│ hello world    ┆ 2          │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ foo bar baz    ┆ 3          │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
│ test123test456 ┆ 1          │
╰────────────────┴────────────╯
(Showing first 3 of 3 rows)
1
>>> df.with_column("digit_count", regexp_count(df["x"], r"\d+")).collect()
╭────────────────┬─────────────╮
│ x              ┆ digit_count │
│ ---            ┆ ---         │
│ String         ┆ UInt64      │
╞════════════════╪═════════════╡
│ hello world    ┆ 0           │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ foo bar baz    ┆ 0           │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ test123test456 ┆ 2           │
╰────────────────┴─────────────╯
(Showing first 3 of 3 rows)
Source code in daft/functions/str.py
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
def regexp_count(
    expr: Expression,
    pattern: str | Expression,
) -> Expression:
    r"""Counts the number of times a regex pattern appears in a string.

    Args:
        expr: The expression to check.
        pattern: The regex pattern to search for as a string or as a column to pick values from.

    Returns:
        Expression: An UInt64 expression with the count of regex matches for each string.

    Examples:
        >>> import daft
        >>> from daft.functions import regexp_count
        >>> df = daft.from_pydict({"x": ["hello world", "foo bar baz", "test123test456"]})
        >>> df.with_column("word_count", regexp_count(df["x"], r"\w+")).collect()
        ╭────────────────┬────────────╮
        │ x              ┆ word_count │
        │ ---            ┆ ---        │
        │ String         ┆ UInt64     │
        ╞════════════════╪════════════╡
        │ hello world    ┆ 2          │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ foo bar baz    ┆ 3          │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ test123test456 ┆ 1          │
        ╰────────────────┴────────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

        >>> df.with_column("digit_count", regexp_count(df["x"], r"\d+")).collect()
        ╭────────────────┬─────────────╮
        │ x              ┆ digit_count │
        │ ---            ┆ ---         │
        │ String         ┆ UInt64      │
        ╞════════════════╪═════════════╡
        │ hello world    ┆ 0           │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ foo bar baz    ┆ 0           │
        ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
        │ test123test456 ┆ 2           │
        ╰────────────────┴─────────────╯
        <BLANKLINE>
        (Showing first 3 of 3 rows)

    """
    return Expression._call_builtin_scalar_fn("regexp_count", expr, pattern)