import re


def get_vb_contents(vba_code):
    """
    Pull out Visual Basic code from .hta file contents.
    """

    # Pull out the VB code.
    pat = r"<\s*[Ss][Cc][Rr][Ii][Pp][Tt]\s+(?:(?:[Ll][Aa][Nn][Gg][Uu][Aa][Gg][Ee])|(?:[Tt][Yy][Pp][Ee]))\s*=\s*\".{0,10}[Vv][Bb][Ss][Cc][Rr][Ii][Pp][Tt]\"\s*>(.{20,})</\s*[Ss][Cc][Rr][Ii][Pp][Tt][^>]*>"
    code = re.findall(pat, vba_code, re.DOTALL)

    # Did we find any VB code in a script block?
    #print code
    if (len(code) == 0):
        return vba_code

    # We have script block VB code.    
    
    # Return the code.
    r = ""
    for b in code:
        if ("</script>" in b):
            b = b[:b.index("</script>")]
        r += b + "\n"
    return r