{"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":"validation","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/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/functions/escape_regex.py#L6-L30","documentation":"The standalone `polars.escape_regex` function operates on plain Python strings only. Passing a polars Expr raises this TypeError pointing to the expression-level method `Expr.str.escape_regex`, which is the correct API for expressions.","triggerScenarios":"Calling pl.escape_regex(pl.col('text')) or pl.escape_regex(any expression) inside a select/with_columns context.","commonSituations":"Misreading the docs and using the free function inside an expression context; migrating code from a plain-string helper into a DataFrame pipeline unchanged.","solutions":["Use the method form: pl.col('text').str.escape_regex() inside expressions","Reserve pl.escape_regex(...) for plain Python str values","Split the pipeline: escape plain strings before building expressions"],"exampleFix":"// before\ndf.select(pl.escape_regex(pl.col('txt')))\n// after\ndf.select(pl.col('txt').str.escape_regex())","handlingStrategy":"type-guard","validationCode":"if isinstance(value, pl.Expr):\n    value = value.str.escape_regex()\nelse:\n    value = pl.escape_regex(value)","typeGuard":"def is_polars_expr(x) -> bool:\n    return isinstance(x, pl.Expr)","tryCatchPattern":"try:\n    escaped = pl.escape_regex(val)\nexcept TypeError as e:\n    if 'Expr' in str(e):\n        escaped = val.str.escape_regex()\n    else:\n        raise","preventionTips":["Use .str.escape_regex on columns, pl.escape_regex only on plain strings","Search codebase for pl.escape_regex(pl.col pattern in reviews","Keep string-cleaning helpers split into scalar vs expression variants"],"tags":["polars","regex","expr","api-misuse"],"backgroundTag":"unsupported-operation","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}