Help with regexp please

Christopher Subich spam.csubich+block at block.subich.spam.com
Fri Jul 22 00:26:16 EDT 2005


Scott David Daniels wrote:
> Felix Collins wrote:
>> I have an "outline number" system like
>> 1
>> 1.2
>> 1.2.3
>> I want to parse an outline number and return the parent.
> 
> Seems to me regex is not the way to go:
>     def parent(string):
>         return string[: string.rindex('.')]

Absolutely, regex is the wrong solution for this problem.  I'd suggest 
using rsplit, though, since that will Do The Right Thing when a 
top-level outline number is passed:
def parent(string):
    return string.rsplit('.',1)[0]

Your solution will throw an exception, which may or may not be the right 
behaviour.



More information about the Python-list mailing list