Splitting on a regex w/o consuming delimiter
Lars Kellogg-Stedman
lars at larsshack.org
Sun Nov 11 02:30:43 EST 2001
>> Given a string such as:
>>
>> sample = 'one two @three @four five @six'
>>
>> I want to split it on the '@' character, but I want the '@'
>character to be
>> retained in each sequence. That is, I'd like the above string split
>into:
>>
>> one two
>> @three
>> @four five
>> @six
>
>You could do '@'.split(sample)' (which deletes at) and then add back
>to all but first.
I knew I should have said it the first time: this is a contrived example;
what if the delimiter regex was something like '\s(!|@)\s'? Given:
foo ! bar @ baz @ xyzzy ! mumble
You'd end up with:
[ 'foo', 'bar', 'baz', 'xyzzy', 'mumble' ]
With no way of recovering the delimeter.
Really, I just want to be able to split on (?=pattern), or some other
method of splitting a string without consuming the delimiter.
-- Lars
--
Lars Kellogg-Stedman <lars at larsshack.org> --> http://www.larsshack.org/
More information about the Python-list
mailing list