Skip to content

Count all successive non-overlapping regex matches

Source code

Description

Count all successive non-overlapping regex matches

Usage

<Expr>$str$count_matches(pattern, ..., literal = FALSE)

Arguments

pattern A character or something can be coerced to a string Expr of a valid regex pattern, compatible with the regex crate.
Ignored.
literal Logical. If TRUE (default), treat pattern as a literal string, not as a regular expression.

Value

Expr of data type UInt32. Returns null if the original value is null.

Examples

library("polars")

df = pl$DataFrame(foo = c("12 dbc 3xy", "cat\\w", "1zy3\\d\\d", NA))

df$with_columns(
  count_digits = pl$col("foo")$str$count_matches(r"(\d)"),
  count_slash_d = pl$col("foo")$str$count_matches(r"(\d)", literal = TRUE)
)
#> shape: (4, 3)
#> ┌────────────┬──────────────┬───────────────┐
#> │ foo        ┆ count_digits ┆ count_slash_d │
#> │ ---        ┆ ---          ┆ ---           │
#> │ str        ┆ u32          ┆ u32           │
#> ╞════════════╪══════════════╪═══════════════╡
#> │ 12 dbc 3xy ┆ 3            ┆ 0             │
#> │ cat\w      ┆ 0            ┆ 0             │
#> │ 1zy3\d\d   ┆ 2            ┆ 2             │
#> │ null       ┆ null         ┆ null          │
#> └────────────┴──────────────┴───────────────┘