polars.internals.expr.ExprStringNameSpace.count_match

ExprStringNameSpace.count_match(pattern: str) polars.internals.expr.Expr

Count all successive non-overlapping regex matches.

Parameters
pattern

A valid regex pattern

Returns
UInt32 array. Contain null if original value is null or regex capture nothing.

Examples

>>> df = pl.DataFrame({"foo": ["123 bla 45 asd", "xyz 678 910t"]})
>>> df.select(
...     [
...         pl.col("foo").str.count_match(r"\d").alias("count_digits"),
...     ]
... )
shape: (2, 1)
┌──────────────┐
│ count_digits │
│ ---          │
│ u32          │
╞══════════════╡
│ 5            │
├╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ 6            │
└──────────────┘