(?:) regular expression Python
Question: I came across a regular expression today but it was very poorly and scarcely
explained. What is the purpose of `(?:)` regex in python and where & when is
it used?

I have tried this but it doesn't seem to be working. Why is that?

    
    
    word = "Hello. ) kahn. ho.w are 19tee,n doing 2day; (x+y)"
    expressoin = re.findall(r'(?:a-z\+a-z)', word);
    


Answer: From the [`re` module
documentation](http://docs.python.org/2/library/re.html):

>
>     (?:...)
>  
>
> A non-capturing version of regular parentheses. Matches whatever regular
> expression is inside the parentheses, but the substring matched by the group
> cannot be retrieved after performing a match or referenced later in the
> pattern.

Basically, it's the same thing as `(...)` but without storing a captured
string in a group.

Demo:

    
    
    >>> import re
    >>> re.search('(?:foo)(bar)', 'foobar').groups()
    ('bar',)
    

Only one group is returned, containing `bar`. The `(?:foo)` group was not.

Use this whenever you need to group metacharacters that would otherwise apply
to a larger section of the expression, such as `|` alternate groups:

    
    
    monty's (?:spam|ham|eggs)
    

You don't need to capture the group but do need to limit the scope of the `|`
meta characters.

As for your sample attempt; using `re.findall()` you often **do** want to
capture output. You most likely are looking for:

    
    
    re.findall('([a-z]\+[a-z])', word)
    

where `re.findall()` will return a list tuples of all captured groups; if
there is only one captured group, it's a list of strings containing just the
one group per match.

Demo:

    
    
    >>> word = "Hello. ) kahn. ho.w are 19tee,n doing 2day; (x+y)"
    >>> re.findall('([a-z]\+[a-z])', word)
    ['x+y']
    

