VariableDefinition = re.compile('<%(["\\\']?)(.*?)\\1=(["\\\']?)(.*?)\\3%>',
    re.LOCALE)
VarRepl = re.compile('<%(["\\\']?)(.*?)\\1%>', re.LOCALE)

def processVariables(item):
    vars = {}

def findVars(m):
    vars[m.group(2).upper()] = m.group(4)
return ''
item = VariableDefinition.sub(findVars, item)
return VarRepl.sub(lambda m: vars[m.group(2).upper()], item)
print(processVariables(
    '<%"TITLE"="This Is A Test Variable"%>The Web <%"TITLE"%>'))
I'm using Python regexes in a criminally inefficient manner