I assume the best way to do this is with regex but I do not know how to do it. I am trying to parse a string and put a space between letters and punctuation only. I want to keep punctuation marks together. As an example if I have the string
"yes!!!"
I want to end up with
"yes", "!!!".
If I have the string
!!!N00bs,
I want to end up with
"!!!", "N00bs"
Is this possible? What is the best way to do this? Right now I am parsing each letter and it a silly way of doing it.
Thanks for the help.
something like this:
txt = re.sub( r'([a-zA-Z])([,.!])', r'\1 \2', '!!!this, .is, .a .test!!!' )
you can switch the order for the other direction
re.sub( r'([,.!])([a-zA-Z])', r'\1 \2', txt )
probably you can also make it work in one regex as well
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