I\'m using the RE expression in python and trying to split a chunk of text by period and by exclamation mark. However when I split it, I get a \"None\" in the result
<
it is happening because after every exclamation mark there's a space character which is returned as None
here.
You can use filter to remove these None
's.
>>> import re
>>> a = "This is my text...I want it to split by periods. I also want it to split \
by exclamation marks! Is that so much to ask?"
>>> filter(lambda x:x!=None, re.split('((?<=\w)\.(?!\..))|(!)',a))
['This is my text...I want it to split by periods', '.', ' I also want it to split by exclamation marks', '!', ' Is that so much to ask?']