Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101766

Re: Powerful perl paradigm I don't find in python

From Nathan Hilterbrand <nhilterbrand@gmail.com>
Newsgroups comp.lang.python
Subject Re: Powerful perl paradigm I don't find in python
Date 2016-01-15 11:54 -0500
Message-ID <mailman.23.1452876885.15297.python-list@python.org> (permalink)
References <n7adse$k6$1@dont-email.me>

Show all headers | View raw



On 01/15/2016 04:24 AM, Charles T. Smith wrote:
> while ($str != $tail) {
>      $str ~= s/^(head-pattern)//;
>      use ($1);
> }

IDK...  maybe the OP is looking for something like this? :

import re

def do_something(matchobj):
     print("I found {}".format(matchobj.group(0)))
     return ""

tail = "END"
str = "FeeFieFooFumEND"
pattern = r"F.."

while(str and str != tail):
     oldstr = str
     str = re.sub(pattern, do_something, str, 1)
     if str == oldstr:
         break


Though I would probably change the perl code, too:

while ($str and $str != $tail) {
     $str ~= s/^(head-pattern)//;
     if ($1) {
        do_something($1);
     } else {
        last;
     }
}

Otherwise there is too much risk of an infinite loop if the string is 
(1) empty, or (2) never ends up being equal to "tail"

Nathan

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Powerful perl paradigm I don't find in python "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-01-15 09:24 +0000
  Re: Powerful perl paradigm I don't find in python Peter Otten <__peter__@web.de> - 2016-01-15 10:43 +0100
    Re: Powerful perl paradigm I don't find in python Michael Vilain <vilain@NOspamcop.net> - 2016-01-15 02:20 -0800
  Re: Powerful perl paradigm I don't find in python Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2016-01-15 11:42 +0100
    Re: Powerful perl paradigm I don't find in python "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-01-15 11:04 +0000
      Re: Powerful perl paradigm I don't find in python "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-01-15 11:06 +0000
      Re: Powerful perl paradigm I don't find in python Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2016-01-15 14:20 +0100
        Re: Powerful perl paradigm I don't find in python "Charles T. Smith" <cts.private.yahoo@gmail.com> - 2016-01-18 13:05 +0000
          Re: Powerful perl paradigm I don't find in python Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2016-01-18 14:33 +0100
      Re: Powerful perl paradigm I don't find in python Peter Otten <__peter__@web.de> - 2016-01-15 14:34 +0100
  Re: Powerful perl paradigm I don't find in python Ulli Horlacher <framstag@rus.uni-stuttgart.de> - 2016-01-15 13:51 +0000
    Re: Powerful perl paradigm I don't find in python me <self@example.org> - 2016-01-15 15:20 +0000
  Re: Powerful perl paradigm I don't find in python Nathan Hilterbrand <nhilterbrand@gmail.com> - 2016-01-15 11:54 -0500

csiph-web