In Python 3.x, the special re sequence '\s' matches Unicode whitespace characters including [ \t\n\r\f\v].
The following piece of code is intended to replace tabs and newlines with a space.
import re
text = """Hello my friends.
How are you doing?
I'm fine."""
output = re.sub('\s', ' ', text)
print(output)
However, the tab is still present in output. Why?
"\n" matches a newline character.
If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")
You can remove any character/string from a string by using the Replace function. This allows you to specify the string you want to replace, and the value you want to replace it with. When you want to "remove" you simple use an empty string as the replace value.
The problem is(likely) that your tab character is just a bunch of spaces.
>>> re.sub(r"\s+", " ", text)
"Hello my friends. How are you doing? I'm fine."
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