def replace_identifier(s, old, new):
    """
    Replace any occurance of the Python identifier `old` with `new` in
    the given string `s` -- but do *not* modify any occurances of
    `old` that occur inside of string literals or comments.  This
    could be used, e.g., for variable renaming.
    """
    # A regexp that matches comments, strings, and `old`.
    comment_re = r'\#.*'
    regexp = re.compile(r'(?x)%s|%s|(?P<old>\b%s\b)' %
                        (comment_re, PY_STRING_LITERAL_RE, re.escape(old)))

    # A callback used to find the replacement value for each match.
    def repl(match):
        if match.group('old'):
            # We matched `old`:
            return new 
        else:
            # We matched a comment or string literal:
            return match.group()

    # Find an regexp matches, and use `repl()` to find the replacement
    # value for each.  Since re.sub only replaces leftmost
    # non-overlapping occurances, occurances of `old` inside strings
    # or comments will be matched as part of that string or comment,
    # and so won't be changed.
    return regexp.sub(repl, s)

    Replace any occurance of the Python identifier `old` with `new` in
    the given string `s` -- but do *not* modify any occurances of
    `old` that occur inside of string literals or comments.  This
    could be used, e.g., for variable renaming.
    
recipes/Python/475109_Regular_expressipythstring/recipe-475109.py