I'm trying tokenize some code and would like to keep the delimiter when I split a string.
For example, I would like to keep any occurences of .
, (
, )
, ;
, ~
.
I have been using re.split:
line = 'Keyboard.keyPressed();'
re.split(r'([\.\(\)\;\~])', line)
However, my current implementation of re.split currently creates some unnecessary empty strings in the array:
['Keyboard', '.', 'keyPressed', '(', '', ')', '', ';', '']
How can I fix this to exclude the empty strings?