Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Wolfgang Maier Newsgroups: comp.lang.python Subject: Re: Powerful perl paradigm I don't find in python Date: Fri, 15 Jan 2016 14:20:17 +0100 Lines: 49 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 8EX4hJ1RYM7VRqPkziYt+Q9W6Use0StwD1ERSSVr85gA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'matches': 0.07; 'see.': 0.07; 'valueerror:': 0.07; 'cursor': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'snippet': 0.09; 'subject:don': 0.09; 'jan': 0.11; 'subject:python': 0.14; '*and': 0.16; '2016': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'simplifies': 0.16; 'substitute': 0.16; 'traverse': 0.16; 'wrote:': 0.16; 'string': 0.17; 'try:': 0.18; '>>>': 0.20; 'essential': 0.20; 'class,': 0.22; 'parse': 0.22; 'pass': 0.22; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'feature': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'fri,': 0.27; 'perl': 0.29; 'received:132': 0.29; 'tail': 0.29; 'allows': 0.30; 'guess': 0.31; 'maintaining': 0.34; 'except': 0.34; 'best,': 0.35; 'something': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'things': 0.38; 'skip:p 20': 0.38; 'does': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'avoid': 0.61; 'more': 0.63; 'cut': 0.67; 'smith': 0.76; 'actually,': 0.84; 'otten': 0.84; 'subject:find': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 132.230.195.61 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101751 On 15.01.2016 12:04, Charles T. Smith wrote: > On Fri, 15 Jan 2016 11:42:24 +0100, Wolfgang Maier wrote: > >> On 15.01.2016 10:43, Peter Otten wrote: >>> Charles T. Smith wrote: >>> >>>> while ($str != $tail) { >>>> $str ~= s/^(head-pattern)//; >>>> use ($1); >>>> } >>> > .... >> >> things = [] >> while some_str != tail: >> m = re.match(pattern_str, some_str) >> things.append(some_str[:m.end()]) >> some_str = some_str[m.end():] >> >> # do something with things > > > Okay, I guess it's not a lot more work to use the end() method to manually > cut out the found portion. > > What the original snippet does is parse *and consume* a string - actually, > to avoid maintaining a cursor traverse the string. The perl feature is that > substitute allows the found pattern to be replaced, but retains the group > after the expression is complete. > > The end() method is actually such a cursor, but already set up for you > by the class, and then the slicing considerably simplifies its use. > I see. If consuming the string is not essential for you, but just a handy trick to avoid the cursor, you may prefer this (most likely faster) solution: pattern = pattern_str.compile() try: matches = pattern.findall(some_str, endpos=some_str.index(tail)) except ValueError: # do something if tail is not found pass Best, Wolfgang