import re


def apply_correction(message, pattern, replacement):
    """
    Replaces all occurences of pattern in message with replacment.  It tries to
    treat the pattern and replacement as regular expressions, but falls back to
    string replace if that fails.
    """

    try:
        message = re.compile(pattern).sub(replacement, message)
    except:
        message = message.replace(pattern, replacement)

    return message