How to retrieve the value using a regular expression in Python?
Question: I wrote code like this:

    
    
    print re.findall(r'(<td width="[0-9]+[%]?" align="(.+)">|<td align="(.+)"> width="[0-9]+[%]?")([ \n\t\r]*)([0-9,]+\.[0-9]+)([ \n\t\r]*)([&]?[a-zA-Z]+[;]?)([ \n\t\r]*)<span class="(.+)">',r.text,re.MULTILINE)
    

to get this line:

    
    
    <td width="47%" align="left">556.348&nbsp;<span class="uccResCde">
    

I want the value 556.348. How I can get it using [regular
expressions](http://en.wikipedia.org/wiki/Regular_expression)?


Answer: A straight cut and paste from [the HTMLParser
documentation](http://docs.python.org/library/htmlparser.html) will get you
the data out of the tags, but not using regular expressions.

    
    
    from HTMLParser import HTMLParser
    
    # Create a subclass and override the handler methods
    class MyHTMLParser(HTMLParser):
        def handle_starttag(self, tag, attrs):
            print "Encountered a start tag:", tag
        def handle_endtag(self, tag):
            print "Encountered an end tag :", tag
        def handle_data(self, data):
            print "Encountered some data  :", data
    
    # Instantiate the parser and fed it some HTML
    parser = MyHTMLParser()
    parser.feed('<td width="47%" align="left">556.348&nbsp;<span class="uccResCde">')
    

