polars.Series.str.contains#

Series.str.contains(pattern: str | Expr, literal: bool = False, strict: bool = True) Series[source]#

Check if strings in Series contain 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.

Returns:
Boolean mask

Examples

>>> s = pl.Series(["Crab", "cat and dog", "rab$bit", None])
>>> s.str.contains("cat|bit")
shape: (4,)
Series: '' [bool]
[
    false
    true
    true
    null
]
>>> s.str.contains("rab$", literal=True)
shape: (4,)
Series: '' [bool]
[
    false
    false
    true
    null
]