import re


def sub_if(ex, repl, string, flags):
    """
    Attempt a substitutuion and return the substituted string if there were
    matches.
    """
    res = re.sub(ex, repl, string, count=1, flags=flags)
    if res != string:
        return res