string split without consumption

Tim Chase python.list at tim.thechases.com
Sat Feb 2 10:42:22 EST 2008


>>> this didn't work elegantly as expected:
>>>
>>>  >>> ss
>>> 'owi\nweoifj\nfheu\n'
>>>  >>> re.split(r'(?m)$',ss)
>>> ['owi\nweoifj\nfheu\n']
>> Do you have a need to use a regexp?
> 
> I'd like the general case - split without consumption.

I'm not sure there's a one-pass regex solution to the problem
using Python's regex engine.  If pre-processing was allowed, one
could do it.

>>>>> ss.splitlines(True)
>> ['owi\n', 'weoifj\n', 'fheu\n']
>>
> 
> thanks. Yet this does not work "naturally" consistent in my line 
> processing algorithm - the further buffering. Compare e.g. 
> ss.split('\n')  ..

well, one can do

  >>> [line + '\n' for line in ss.splitlines()]
  ['owi\n', 'eoifj\n', 'heu\n']
  >>> [line + '\n' for line in (ss+'xxx').splitlines()]
  ['owi\n', 'eoifj\n', 'heu\n', 'xxx\n']

as another try for your edge case.  It's understandable and
natural-looking

-tkc







More information about the Python-list mailing list