regular expression i'm going crazy

andy baxter andy at earthsong.free-online.co.uk
Mon May 16 13:11:01 EDT 2011


On 16/05/11 17: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?
> i want my re_s to find linka and la, he just find link and l and forget
> about the ending a.

The round brackets define a 'capturing group'. I.e. when you do findall 
it returns those elements in the string that match what's inside the 
brackets. If you want to get linka and la, you need something like this:

 >>> re_s = re.compile(r'((link|l)a)' , re.IGNORECASE)
 >>> print re_s.findall(s)
[('linka', 'link'), ('la', 'l')]

Then just look at the first element in each of the tuples in the array 
(which matches the outside set of brackets).

see:
http://www.regular-expressions.info/python.html



More information about the Python-list mailing list