Python regex find two groups
Question: 
    >>> text = '<a data-lecture-id="47"\n   data-modal-iframe="https://class.coursera.org/neuralnets-2012-001/lecture/view?lecture_id=47"\n   href="https://class.coursera.org/neuralnets-2012-001/lecture/47"\n   data-modal=".course-modal-frame"\n   rel="lecture-link"\n   class="lecture-link">\nAnother diversion: The softmax output function [7 min]</a>'
    
    >>> import re
    >>> re.findall(r'data-lecture-id="(\d+)"|(.*)</a>',a)
    >>> [('47', ''), ('', 'Another diversion: The softmax output function [7 min]')]
    

How do i extract the data out like this:

    
    
    >>> ['47', 'Another diversion: The softmax output function [7 min]']
    

I think there should be some smarter regex expressions.


Answer: It is [not recommended to parse HTML with reguar
expressions](http://stackoverflow.com/questions/590747/using-regular-
expressions-to-parse-html-why-not). You can give a try to the
[`xml.dom.minidom`](http://docs.python.org/2/library/xml.dom.minidom.html)
module:

    
    
    from xml.dom.minidom import parseString
    
    xml = parseString('<a data-lecture-id="47"\n   data-modal-iframe="https://class.coursera.org/neuralnets-2012-001/lecture/view?lecture_id=47"\n   href="https://class.coursera.org/neuralnets-2012-001/lecture/47"\n   data-modal=".course-modal-frame"\n   rel="lecture-link"\n   class="lecture-link">\nAnother diversion: The softmax output function [7 min]</a>')
    anchor = xml.getElementsByTagName("a")[0]
    print anchor.getAttribute("data-lecture-id"), anchor.childNodes[0].data
    

