import re


def replace_token(line, str1, str2):
    """Replace token str1 with new str2 in line."""
    # pull out what you want to keep on left and right
    # rather than capture what you want and replace it
    if str1 not in line:
        return line
    pattern = r'(^|.*[^a-zA-Z_]+)' + str1 + r'($|[^a-zA-Z0-9_]+.*)'
    line = re.sub(pattern, r'\1' + str2 + r'\2', line)
    return line