Powerful perl paradigm I don't find in python

Charles T. Smith cts.private.yahoo at gmail.com
Fri Jan 15 06:04:32 EST 2016


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.

The point is, it would have been easy for python to offer the same
capability, somehow, but that was apparently overlooked.  For example,
by storing string state in the match object and having a method without a
string parameter.





More information about the Python-list mailing list