Skip to content

Select all columns that match the given regex pattern

Description

Select all columns that match the given regex pattern

Usage

cs__matches(pattern)

Arguments

pattern A valid regular expression pattern, compatible with the regex crate \\_.

Value

A Polars selector

See Also

cs for the documentation on operators supported by Polars selectors.

Examples

library("polars")

df <- pl$DataFrame(
  foo = c("x", "y"),
  bar = c(123, 456),
  baz = c(2.0, 5.5),
  zap = c(0, 1)
)

# Match column names containing an "a", preceded by a character that is not
# "z":
df$select(cs$matches("[^z]a"))
#> shape: (2, 2)
#> ┌───────┬─────┐
#> │ bar   ┆ baz │
#> │ ---   ┆ --- │
#> │ f64   ┆ f64 │
#> ╞═══════╪═════╡
#> │ 123.0 ┆ 2.0 │
#> │ 456.0 ┆ 5.5 │
#> └───────┴─────┘
# Do not match column names ending in "R" or "z" (case-insensitively):
df$select(!cs$matches(r"((?i)R|z$)"))
#> shape: (2, 2)
#> ┌─────┬─────┐
#> │ foo ┆ zap │
#> │ --- ┆ --- │
#> │ str ┆ f64 │
#> ╞═════╪═════╡
#> │ x   ┆ 0.0 │
#> │ y   ┆ 1.0 │
#> └─────┴─────┘