import re


def _strip_identifiers_build_suffix(identifier):
    """
    Return a stripped chart version or image tag (identifier) without its build
    suffix (.005.asdf1234), leaving it to represent a Semver 2 release or
    pre-release.

    Example:
        identifier: "0.1.2-005.asdf1234"            returns: "0.1.2"
        identifier: "0.1.2-alpha.1.005.asdf1234"    returns: "0.1.2-alpha.1"
    """
    # split away official SemVer 2 build specifications if used
    if "+" in identifier:
        return identifier.split("+", maxsplit=1)[0]

    # split away our custom build specification: something ending in either
    # . or - followed by three or more digits, a dot, an commit sha of four
    # or more alphanumeric characters.
    return re.sub(r'[-\.]\d{3,}\.\w{4,}\Z', "", identifier)