I have a sentence with many special characters and text in it, I want remove all the special characters except dot and comma.
For example, this is what have:
[u' %$HI# Jhon, $how$ are *&$%you.%$
I'm trying to produce the following string:
HI Jhon, how are you.
I tried this
("[u' %$HI# Jhon, $how$ are *&$%you.%$").replaceAll("[^a-zA-Z]+","");
But it removes commas and dots also. I want commas and dots to be there.
Finally i found solution:
Python:
import re
my_str = "[u' %$HI# Jhon, $how$ are *&$%you.%$"
my_new_string = re.sub('[^.,a-zA-Z0-9 \n\.]', '', my_str)
print (my_new_string)
Java:
("[u' %$HI# Jhon, $how$ are *&$%you.%$").replaceAll("[^ .,a-zA-Z0-9]");
Thanks all. I Don't know whats wrong with my question, Don't have freedom to ask. :-(
("[u' %$HI# Jhon, $how$ are *&$%you.%$").replace(/[^.,a-zA-Z]/g, '');
You need to add comma and dot with all characters inside the brackets, like I just did.
And you might want to include numbers too.
("[u' %$HI# Jhon, $how$ are *&$%you.%$").replace(/[^.,a-zA-Z0-9]/g, '');
Edited
And, as noted below, your output also needs spaces:
("[u' %$HI# Jhon, $how$ are *&$%you.%$").replace(/[^.,a-zA-Z ]/g, '');
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