Referencing a RegEx Variable
Question: I'm using python to loop through a large list of self reported locations to
try to match them to their home states. The RegEx expression I'm using is:

    
    
    /^"[^\s]+,\s*([a-zA-Z]{2})"$/
    

Basically, I'm trying to find a pattern that looks like `XXXCITYXXX,
[Statecode]`, where _statecode_ is only two letters.

My issue is that I don't know how to reference the varying state code once I
find a matching string. I know in Perl that I could use:

    
    
    $state = uc($1)
    

However, I don't know the equivalent Python syntax. Anyone know?


Answer: You can do it with re.search, which returns a `match` object (if the regex
matches at all) with a `groups` property containing the captured groups:

    
    
    import re
    match = re.search('^[^\s]+,\s*([a-zA-Z]{2})$', my_string)
    if match:
        print match.groups()[0]
    

