Skip to content

Docs: behavior of regular expression pattern caching is unclear #139316

@sethdandridge

Description

@sethdandridge

Documentation

The current documentation for the re.compile function mentions that compiled regular expressions are cached, but the wording could be more precise to clarify that this caching also applies to patterns passed as strings to other module functions (e.g., re.search, re.match). This change would help guide developers away from unnecessarily calling re.compile.

This abbreviated code snippet demonstrates the behavior I am referring to:

import re

re.match(r"fo+", "fooooooo")

for cache_entry in re._cache.items():
    print(cache_entry)

#> ((<class 'str'>, 'fo+', 0), re.compile('fo+'))

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirpendingThe issue will be closed if no feedback is providedtopic-regex

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions