In Python, how do I split a string and keep the separators?

>>> re.split('(\W)', 'foo/bar spam\neggs')
['foo', "https://stackoverflow.com/", 'bar', ' ', 'spam', '\n', 'eggs']

Leave a Comment