Using regex to find data in Python
Question: I am new to python, and developing in general. Let me give an example what I
am trying to do.

I want to find the text name="username" type="hidden" value="blah" and I only
want to pull the "blah"

How would I begin to go about that?


Answer: You can use [regex
groups](http://docs.python.org/library/re.html#re.MatchObject.group) to pick
out relevant parts of a match.

    
    
    #!/usr/bin/env python
    
    s = """ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
    eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
    veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
    commodo consequat. 
    name="username" type="hidden" value="blah" 
    Duis aute irure dolor in reprehenderit in voluptate velit
    esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat
    non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    """
    
    import re
    
    pattern = re.compile(r'name="username"\stype="hidden"\svalue="([^"]*)"')
    for match in pattern.finditer(s):
        print match.group(1)
        # => blah
    

