Skip to content

Use the Aho-Corasick algorithm to find matches

Source code

Description

This function determines if any of the patterns find a match.

Usage

<Expr>$str$contains_any(patterns, ..., ascii_case_insensitive = FALSE)

Arguments

patterns String patterns to search. Accepts expression input. In Polars 1.16, bare character vectors are interpreted as literal patterns; in Polars 2.0, they will be interpreted as column names. Use pl$lit(…)$implode() for literal patterns or pl$col() for column patterns. To use the same character vector for all rows, use list(c(…)) instead of c(…) (see Examples).
These dots are for future extensions and must be empty.
ascii_case_insensitive Enable ASCII-aware case insensitive matching. When this option is enabled, searching will be performed without respect to case for ASCII letters (a-z and A-Z) only.

Value

A polars expression

See Also

  • \$str$contains()

Examples

library("polars")

df <- pl$DataFrame(
  lyrics = c(
    "Everybody wants to rule the world",
    "Tell me what you want, what you really really want",
    "Can you feel the love tonight"
  )
)

df$with_columns(
  contains_any = pl$col("lyrics")$str$contains_any(list(c("you", "me")))
)
#> shape: (3, 2)
#> ┌─────────────────────────────────┬──────────────┐
#> │ lyrics                          ┆ contains_any │
#> │ ---                             ┆ ---          │
#> │ str                             ┆ bool         │
#> ╞═════════════════════════════════╪══════════════╡
#> │ Everybody wants to rule the wo… ┆ false        │
#> │ Tell me what you want, what yo… ┆ true         │
#> │ Can you feel the love tonight   ┆ true         │
#> └─────────────────────────────────┴──────────────┘