Implementing Regular expressions in Python
Question: I have a code like this.

    
    
     <td class="check ABCD" rowspan="2"><center><div class="checkbox {{#if checked}}select{{else}}deselect{{/if}}" id="{{id}}" {{data "tool"}
    
    <td class="check" rowspan="2"><center><div class="checkbox {{#if checked}}select{{else}}deselect{{/if}}" id="{{id}}" {{data "tool"}}>
    

And I want to extract only the class and ID name in the above code. I have
very little knowledge about using regular expression in python.

How can I extract only the class name & id name(the ones in between "") using
regular expression? or is there any better way to do this?. If yes, please
help me finding it :)

Thanks in advance.


Answer: Since you asked for a Regex solution in Python, you'll get one:

    
    
    import re
    p = re.compile(ur'^.+?class="([^"]+)".+id="([^"]+)".+?$', re.MULTILINE)
    test_str = u"<td class=\"check ABCD\" rowspan=\"2\"><center><div class=\"checkbox {{#if checked}}select{{else}}deselect{{/if}}\" id=\"{{id}}\" {{data \"tool\"}\n<td class=\"check\" rowspan=\"2\"><center><div class=\"checkbox {{#if checked}}select{{else}}deselect{{/if}}\" id=\"{{id}}\" {{data \"tool\"}}>"
    
    re.findall(p, test_str)
    

See live example over here: <https://regex101.com/r/cG8dC5/1>

Nevertheless, as some other users already noted. Regex [isn't ideal for
parsing](http://stackoverflow.com/questions/1732348/regex-match-open-tags-
except-xhtml-self-contained-tags/1732454#1732454 "isn't ideal for parsing")
(x)HTML. Better have a look at: <https://pypi.python.org/pypi/beautifulsoup4>

