This is a very basic question, but I am stuck, so any help would be appreciated.
import re
re.split(r'(\d|\W|\_)', ' ab2c d_ef')
['', ' ', '', ' ', 'ab', '2', 'c', ' ', 'd', '_', 'ef']
How do I get rid of the empty '' match? I do need the space character. I want my output to look like below:
[' ', ' ', 'ab', '2', 'c', ' ', 'd', '_', 'ef']
Use re.findall instead of re.split:
>>> import re
>>> re.findall(r'[a-z]+|\d+|_|\s', ' ab2c d_ef')
[' ', ' ', 'ab', '2', 'c', ' ', 'd', '_', 'ef']
You don't need to escape _. It does not have special meaning in regular expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With