polars.Expr.str.contains_any#

Expr.str.contains_any(
patterns: IntoExpr,
*,
ascii_case_insensitive: bool = False,
) Expr[source]#

Use the Aho-Corasick algorithm to find matches.

Determines if any of the patterns are contained in the string.

Parameters:
patterns

String patterns to search.

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.

Notes

This method supports matching on string literals only, and does not support regular expression matching.

Examples

>>> _ = pl.Config.set_fmt_str_lengths(100)
>>> df = pl.DataFrame(
...     {
...         "lyrics": [
...             "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(
...     pl.col("lyrics").str.contains_any(["you", "me"]).alias("contains_any")
... )
shape: (3, 2)
┌────────────────────────────────────────────────────┬──────────────┐
│ lyrics                                             ┆ contains_any │
│ ---                                                ┆ ---          │
│ str                                                ┆ bool         │
╞════════════════════════════════════════════════════╪══════════════╡
│ Everybody wants to rule the world                  ┆ false        │
│ Tell me what you want, what you really really want ┆ true         │
│ Can you feel the love tonight                      ┆ true         │
└────────────────────────────────────────────────────┴──────────────┘