Splitting on a regex w/o consuming delimiter
Emile van Sebille
emile at fenx.com
Sun Nov 11 03:06:38 EST 2001
>>> ", @".join('one two @three @four five @six'.split('@')).split(',')
['one two ', ' @three ', ' @four five ', ' @six']
;-)
--
Emile van Sebille
emile at fenx.com
---------
"Lars Kellogg-Stedman" <lars at larsshack.org> wrote in message
news:slrn9usaaa.4ao.lars at flowers.house.larsshack.org...
> >> 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