Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5528
| Date | 2011-05-16 18:11 +0100 |
|---|---|
| From | andy baxter <andy@earthsong.free-online.co.uk> |
| Subject | Re: regular expression i'm going crazy |
| References | <4dd14fdb$0$18238$4fafbaef@reader2.news.tin.it> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1649.1305565877.9059.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
regular expression i'm going crazy Tracubik <affdfsdfdsfsd@b.com> - 2011-05-16 16:25 +0000 Re: regular expression i'm going crazy Robert Kern <robert.kern@gmail.com> - 2011-05-16 11:51 -0500 Re: regular expression i'm going crazy Alexander Kapps <alex.kapps@web.de> - 2011-05-16 19:01 +0200 Re: regular expression i'm going crazy andy baxter <andy@earthsong.free-online.co.uk> - 2011-05-16 18:11 +0100
csiph-web