{"record":{"id":"10632f078f8b230e","repo":"pola-rs/polars","slug":"escape-regex-function-is-unsupported-for-expr-y","errorCode":null,"errorMessage":"escape_regex function is unsupported for `Expr`, you may want use `Expr.str.escape_regex` instead","messagePattern":"escape_regex function is unsupported for `Expr`, you may want use `Expr\\.str\\.escape_regex` instead","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/functions/escape_regex.py","lineNumber":24,"sourceCode":"\nwith contextlib.suppress(ImportError):  # Module not available when building docs\n    import polars._plr as plr\nimport polars._reexport as pl\n\n\ndef escape_regex(s: str) -> str:\n    r\"\"\"\n    Escapes string regex meta characters.\n\n    Parameters\n    ----------\n    s\n        The string whose meta characters will be escaped.\n\n    \"\"\"\n    if isinstance(s, pl.Expr):\n        msg = \"escape_regex function is unsupported for `Expr`, you may want use `Expr.str.escape_regex` instead\"\n        raise TypeError(msg)\n    elif not isinstance(s, str):\n        msg = f\"escape_regex function supports only `str` type, got `{qualified_type_name(s)}`\"\n        raise TypeError(msg)\n\n    return plr.escape_regex(s)\n","sourceCodeStart":6,"sourceCodeEnd":30,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/functions/escape_regex.py#L6-L30","documentation":"Top-level pl.escape_regex escapes regex metacharacters of a plain Python string so it can be embedded safely in a pattern. It operates on host strings only; passing an Expr is a common slip when column-wise escaping is wanted, so the error explicitly redirects to Expr.str.escape_regex.","triggerScenarios":"pl.escape_regex(pl.col('s')); pl.escape_regex(some_expr) inside a select/with_columns pipeline where per-row escaping was intended.","commonSituations":"Building a contains pattern from user input and mistakenly escaping a column instead of the literal; refactoring from string constants to columns.","solutions":["For column-wise escaping use the expression namespace: pl.col('s').str.escape_regex()","Keep pl.escape_regex for plain str literals you splice into a pattern: pl.col('x').str.contains(pl.escape_regex(user_input) + '.*')"],"exampleFix":"# before\npl.escape_regex(pl.col('s'))  # TypeError\n\n# after\npl.col('s').str.escape_regex()","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def safe_escape_regex(s):\n    if isinstance(s, pl.Expr):\n        return s.str.escape_regex()  # column-wise\n    return pl.escape_regex(s)  # plain string","tryCatchPattern":null,"preventionTips":["Reserve the top-level function for plain strings; use the .str namespace on columns","Annotate parameters as str so type checkers catch Expr leaks"],"tags":["polars","escape-regex","expr","api-misuse"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}