Powerful perl paradigm I don't find in python

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Fri Jan 15 08:20:17 EST 2016


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





More information about the Python-list mailing list