import re


def Match_Pentagon(text):
    """
    ABCDE -> Pentagon(A,B,C,D,E)
    Pentagon ABCDE -> Pentagon(A,B,C,D,E)
    """
    pattern = re.compile(
        r'(?:[P|p]olygon |)([P|p]entagon )?(?<![A-Z])([A-Z])([A-Z])([A-Z])([A-Z])([A-Z])(?![A-Z])(?=[\.\,\?\s])')
    target = r'[Pentagon(\2,\3,\4,\5,\6)]'
    text = re.sub(pattern, target, text)

    # a regular pentagon -> Pentagon($)
    pattern = re.compile(r'([p|P]entagon)(?=[\.\,\?\s])')
    target = r'[Pentagon($)]'
    text = re.sub(pattern, target, text)

    return text