regular expression i'm going crazy

Alexander Kapps alex.kapps at web.de
Mon May 16 13:01:35 EDT 2011


On 16.05.2011 18:25, Tracubik wrote:
> pls help me fixing this:
>
> import re
> s = "linka la baba"
> re_s = re.compile(r'(link|l)a' , re.IGNORECASE)
>
> print re_s.findall(s)
>
> output:
> ['link', 'l']
>
> why?

As the docs say:

"If one or more groups are present in the pattern, return a list of 
groups;"

http://docs.python.org/library/re.html?highlight=findall#re.findall

> i want my re_s to find linka and la, he just find link and l and forget
> about the ending a.

Try with non-grouping parentheses:

re_s = re.compile(r'(?:link|l)a' , re.IGNORECASE)



More information about the Python-list mailing list