polars.Expr.str.contains#
- Expr.str.contains(pattern: str | Expr, literal: bool = False, strict: bool = True) 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.
- strict
Raise an error if the underlying pattern is not a valid regex expression, otherwise mask out with a null value.
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 │ └─────────────┴───────┴─────────┘