Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'retrieved': 0.05; 'python': 0.07; '"""': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'referenced': 0.09; 'substring': 0.09; 'underlying': 0.09; 'this:': 0.11; 'am,': 0.14; 'wrote:': 0.14; 'capturing': 0.16; 'enigma': 0.16; 'expression)': 0.16; 'kern': 0.16; 'matched': 0.16; 'parentheses': 0.16; 'parentheses,': 0.16; 'subject:expression': 0.16; 'subject:regular': 0.16; 'interpret': 0.19; 'specifies': 0.19; 'performing': 0.22; 'header:In-Reply-To:1': 0.22; 'trying': 0.23; 'explore': 0.23; 'group,': 0.24; 'version': 0.25; 'guess': 0.26; 'later': 0.26; 'matches': 0.29; 'match': 0.31; 'url:library': 0.31; 'anyone': 0.31; 'entry': 0.32; 'import': 0.32; 'to:addr :python-list': 0.32; 'url:docs': 0.33; 'expression': 0.33; 'header:X-Complaints-To:1': 0.34; 'regular': 0.34; 'received:rr.com': 0.35; 'print': 0.35; 'header:User-Agent:1': 0.35; 'fixing': 0.35; 'rest': 0.37; 'should': 0.37; 'url:python': 0.37; 'but': 0.38; 'url:org': 0.38; 'received:org': 0.38; 'though': 0.38; 'help': 0.39; 'me?': 0.39; 'ok,': 0.39; 'to:addr:python.org': 0.39; 'header:Mime-Version:1': 0.39; 'received:24': 0.39; 'whatever': 0.39; 'attempt': 0.40; 'header:Received:5': 0.40; 'here:': 0.61; 'link': 0.62; 'our': 0.63; 'world': 0.65; 'believe': 0.66; 'want,': 0.72; '11:25': 0.84; 'eco': 0.84; 'isolate': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Robert Kern Subject: Re: regular expression i'm going crazy Date: Mon, 16 May 2011 11:51:49 -0500 Organization: The Church of Last Thursday References: <4dd14fdb$0$18238$4fafbaef@reader2.news.tin.it> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: rrcs-24-227-244-203.sw.biz.rr.com User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 In-Reply-To: <4dd14fdb$0$18238$4fafbaef@reader2.news.tin.it> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 58 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305564724 news.xs4all.nl 49032 [::ffff:82.94.164.166]:45830 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5526 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