Splitting on a regex w/o consuming delimiter

Lars Kellogg-Stedman lars at larsshack.org
Sun Nov 11 01:28:09 EST 2001


Howdy folks,

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

I thought maybe this would work:

  sre.compile('(?=@)').split(sample)

Since, according to the python documentation, '(?=pattern)' matches
pattern without consuming anything.  While it seemed like a good idea at
the time, it doesn't work.

Other than manually splitting the string, is there a more graceful
solution?

Thanks,

-- Lars

-- 
Lars Kellogg-Stedman <lars at larsshack.org> --> http://www.larsshack.org/




More information about the Python-list mailing list