Finding a combination in a string using regex in python
Question: I'm trying to find a combination but it is not working

    
    
    >>> whole = ('\n1. WIPO is located at\n(A) New York\n(B) London\n(C) Geneva\n(D) Paris\n')
    >>> match = re.findall('1\.\s(\w+\s)+\w+\n', whole)
    >>> print(match)
    ['located']
    

I want it to return `"1. WIPO is located at\n"`. But it is just returning
`['located ']`


Answer: Make the group _non-capturing_ , using `(?:...)` instead of `(...)`:

    
    
    re.findall('1\.\s(?:\w+\s)+\w+\n', whole)
    

If capturing groups are present, `re.findall()` changes behaviour and only
returns the matched groups; with no captured groups the whole match is
returned instead:

    
    
    >>> import re
    >>> whole = '\n1. WIPO is located at\n(A) New York\n(B) London\n(C) Geneva\n(D) Paris\n'
    >>> re.findall('1\.\s(?:\w+\s)+\w+\n', whole)
    ['1. WIPO is located at\n']
    

If you only expect there to be the one match, then don't use `re.findall()`
but use `re.search()` instead, and call `.group()` on the resulting `Match`
instance:

    
    
    >>> re.search('1\.\s(?:\w+\s)+\w+\n', whole).group()
    '1. WIPO is located at\n'
    

Here, it no longer matters if the group is capturing or not.

