pythonregexpep

Regex to match PEP440 compliant version strings


PEP 440 lays out what is the accepted format for version strings of Python packages.

These can be simple, like: 0.0.1

Or complicated, like: 2016!1.0-alpha1.dev2

What is a suitable regex which could be used for finding and validating such strings?


Solution

  • I had the same question. This is the most thorough regex pattern I could find. PEP440 links to the codebase of the packaging library in it's references section.

    pip install packaging
    

    To access just the pattern string you can use the global

    from packaging import version
    version.VERSION_PATTERN
    

    See: https://github.com/pypa/packaging/blob/21.3/packaging/version.py#L225-L254

    VERSION_PATTERN = r"""
        v?
        (?:
            (?:(?P<epoch>[0-9]+)!)?                           # epoch
            (?P<release>[0-9]+(?:\.[0-9]+)*)                  # release segment
            (?P<pre>                                          # pre-release
                [-_\.]?
                (?P<pre_l>(a|b|c|rc|alpha|beta|pre|preview))
                [-_\.]?
                (?P<pre_n>[0-9]+)?
            )?
            (?P<post>                                         # post release
                (?:-(?P<post_n1>[0-9]+))
                |
                (?:
                    [-_\.]?
                    (?P<post_l>post|rev|r)
                    [-_\.]?
                    (?P<post_n2>[0-9]+)?
                )
            )?
            (?P<dev>                                          # dev release
                [-_\.]?
                (?P<dev_l>dev)
                [-_\.]?
                (?P<dev_n>[0-9]+)?
            )?
        )
        (?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))?       # local version
    """
    

    Of course this example is specific to Python's flavor of regex.