I want to split a string using -, +=, ==, =, +, and white-space as delimiters. I want to keep the delimiter
-
+=
==
=
+
Try this:
def tokenize(s): import re pattern = re.compile("(\-|\+\=|\=\=|\=|\+)|\s+") x = pattern.split(s) result = [] for item in x: if item != '' and item != None: result.append(item) return result print(tokenize("hello-+==== =+ there"))