import re


def changeAttribute(tag, attribute, newValue):
    """
    Changes the specified attribute in the specified tag to have the value
    newValue. Returns the newly changed tag and the previous value of the
    attribute.
    """

    regExpression = re.compile('<' + tag[0] + '([^>]* )' +
                               attribute + '= *"(.*)"')

    matchOb = regExpression.search(tag[1])

    if matchOb is not None:
        oldValue = matchOb.group(2)
        resultingString = regExpression.sub(r'<' + tag[0]
                                            + r'\1' + attribute + r'="' + str(newValue) + '"', tag[1])
        #The semicolon is only necessary to prevent vim from miscoloring
        #the text after a sub expression. Should be using emacs...
        tag[1] = resultingString
        return tag, oldValue
    else:
        return None, None