Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #5526
| From | Robert Kern <robert.kern@gmail.com> |
|---|---|
| Subject | Re: regular expression i'm going crazy |
| Date | 2011-05-16 11:51 -0500 |
| Organization | The Church of Last Thursday |
| References | <4dd14fdb$0$18238$4fafbaef@reader2.news.tin.it> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1647.1305564724.9059.python-list@python.org> (permalink) |
On 5/16/11 11:25 AM, 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. > > can anyone help me? trying the regular expression in redemo.py (program > provided with python to explore the use of regular expression) i get what > i want, so i guess re_s is ok, but it still fail... > why? The parentheses () create a capturing group, which specifies that the contents of the group should be extracted. See the "(...)" entry here: http://docs.python.org/library/re#regular-expression-syntax You can use the non-capturing version of parentheses if you want to just isolate the | from affecting the rest of the regex: """ (?:...) 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. """ [~] |1> import re [~] |2> s = "linka la baba" [~] |3> re_s = re.compile(r'(?:link|l)a' , re.IGNORECASE) [~] |4> print re_s.findall(s) ['linka', 'la'] -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
Back to comp.lang.python | Previous | Next — Previous in thread | Next 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