Help with splitting

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Sun Apr 3 07:07:20 EDT 2005


George Sakkis wrote:

> If you don't want any null strings at the beginning or the end, an
> equivalent regexp is:
> 
>>>> whitespaceSplitter_2 = re.compile("\w+|\s+")
>>>> whitespaceSplitter_2.findall("1 2  3   \t\n5")
> ['1', ' ', '2', '  ', '3', '   \t\n', '5']
>>>> whitespaceSplitter_2.findall(" 1 2  3   \t\n5 ")
> [' ', '1', ' ', '2', '  ', '3', '   \t\n', '5', ' ']

Perhaps you may want to use "\s+|\S+" if you have non-alphanumeric
characters in the string.

Reinhold



More information about the Python-list mailing list