import re


def _make_anchor(label, used_anchors):
    """Chooses the appropriate anchor name for a header label."""
    anchor = label.lower().strip()
    # Imitate GFM anchors. Sadly, the exact format isn't clearly documented
    # anywhere, so this based on experimentation and what others have done to
    # successfully replicate the GitHub anchor mapping.
    anchor = anchor.replace(" ", "-")
    anchor = re.sub("[!\"#$%&'()*+,./:;<=>?@[\\\\\\]^`{|}~]", "", anchor)

    # Enumerate anchors when reused.
    if anchor in used_anchors:
        used_anchors[anchor] += 1
        anchor = "%s-%d" % (anchor, used_anchors[anchor])
    else:
        used_anchors[anchor] = 0

    return anchor