polars.internals.expr.string.ExprStringNameSpace.contains#

ExprStringNameSpace.contains(pattern: str, literal: bool = False) Expr[source]#

Check if string contains a substring that matches a regex.

Parameters:
pattern

A valid regex pattern.

literal

Treat pattern as a literal string.

See also

starts_with

Check if string values start with a substring.

ends_with

Check if string values end with a substring.

Examples

>>> df = pl.DataFrame({"a": ["Crab", "cat and dog", "rab$bit", None]})
>>> df.select(
...     [
...         pl.col("a"),
...         pl.col("a").str.contains("cat|bit").alias("regex"),
...         pl.col("a").str.contains("rab$", literal=True).alias("literal"),
...     ]
... )
shape: (4, 3)
┌─────────────┬───────┬─────────┐
│ a           ┆ regex ┆ literal │
│ ---         ┆ ---   ┆ ---     │
│ str         ┆ bool  ┆ bool    │
╞═════════════╪═══════╪═════════╡
│ Crab        ┆ false ┆ false   │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ cat and dog ┆ true  ┆ false   │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ rab$bit     ┆ true  ┆ true    │
├╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┤
│ null        ┆ null  ┆ null    │
└─────────────┴───────┴─────────┘