3

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. :-(

Sai
  • 12,738
  • 15
  • 70
  • 106
  • 1
    Define "special". – Stefan Pochmann Sep 24 '16 at 03:15
  • 1
    @StefanPochmann Good point. I supposed any non-alphanumerical character, but that would also remove the ability to write a question effectively. – AntonH Sep 24 '16 at 03:16
  • 1
    Following edit: why not add dots and commas to the character class in your `replaceAll` of non-acceptable characters to remove? – AntonH Sep 24 '16 at 03:18
  • 2
    Possible duplicate of [How to remove special characters from a string?](http://stackoverflow.com/questions/7552253/how-to-remove-special-characters-from-a-string) – Nikhil Sep 24 '16 at 03:26

3 Answers3

5
("[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, '');
Keith
  • 944
  • 6
  • 9
0

This might also help:

>>> punctuation = """!\"#$%&'()*+-/:;<=>?@[\\]^_`{|}~"""
>>> string = "[%$HI# Jhon, $how$ are *&$%you.%$"
>>> edited = ""
>>> for i in string:
...     if i not in punctuation:
...         edited += i
...
>>> edited
'HI Jhon, how are you.'   
Jeril
  • 5,663
  • 3
  • 39
  • 62
0

assembling a new string w/o the 'special'-characters using lambda [java]

String s = "[u' %$HI# John, $how$ are *&$%you.%$";
s.codePoints().mapToObj( Character::toChars ).filter(
    a -> (a.length == 1 && (Character.isLetterOrDigit( a[0] ) || Character.isSpaceChar( a[0] )
        || a[0] == '.' || a[0] == ',')) )
    .collect( StringBuilder::new, StringBuilder::append, StringBuilder::append ).toString();
//u HI John, how are you.
Kaplan
  • 872
  • 3
  • 7