Method: Polars::StringExpr#count_matches

Defined in:
lib/polars/string_expr.rb

#count_matches(pattern, literal: false) ⇒ Expr

Count all successive non-overlapping regex matches.

Examples:

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

Parameters:

  • pattern (String)

    A valid regex pattern

  • literal (Boolean) (defaults to: false)

    Treat pattern as a literal string, not as a regular expression.

Returns:



1220
1221
1222
1223
# File 'lib/polars/string_expr.rb', line 1220

def count_matches(pattern, literal: false)
  pattern = Utils.parse_into_expression(pattern, str_as_lit: true)
  Utils.wrap_expr(_rbexpr.str_count_matches(pattern, literal))
end