import re


def am_and_pm(sentence):
    """
    Standardize variations as "A.M." or "P.M." iff they appear after a time.
    >>> am_and_pm("at twelve pm")
    'at twelve P.M.'
    >>> am_and_pm("at 12 pm")
    'at 12 P.M.'
    >>> am_and_pm("I am on a boat")
    'I am on a boat'
    >>> am_and_pm("9 am classes")
    '9 A.M. classes'
    >>> am_and_pm("9 AM classes")
    '9 A.M. classes'
    >>> am_and_pm("9:30 AM classes")
    '9:30 A.M. classes'
    >>> am_and_pm("9AM classes")
    '9 A.M. classes'
    >>> am_and_pm("is 280 among")
    'is 280 among'
    """
    number_pattern = r"(?P<time>(^|\s|[A-Za-z:])\d{1,2}|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|fifteen|thirty|forty-?five|o'clock) ?(?P<meridian>"
    am_pattern = number_pattern + r"am|AM|a\.m\.|A\.M\.)"
    pm_pattern = number_pattern + r"pm|PM|p\.m\.|P\.M\.)"

    am_repl = r"\g<time> A.M."
    pm_repl = r"\g<time> P.M."

    sentence = re.sub(am_pattern, am_repl, sentence)
    return re.sub(pm_pattern, pm_repl, sentence)