1110
1110

Reputation: 6839

How to remove all except alphanumeric characters from string

I am trying to remove all special characters from the following string:

abc // t?% ?? ttt ,. y  Ä Ö Ü ä, ö !

With regex:

Regex rgx = new Regex("[^a-zA-Z0-9 -]");

But this regex also remove Ä Ö Ü ä, ö but I want to keep those characters. I want to remove only characters like: !@#$%^&,;:'....

Upvotes: 0

Views: 1158

Answers (2)

dcastro
dcastro

Reputation: 68740

Func<char, bool> filter = ch => char.IsLetterOrDigit(ch) ||
                                char.IsWhiteSpace(ch) ||
                                ch == '-';

var abc = new string(str.Where(filter).ToArray());

Fiddle: https://dotnetfiddle.net/MBRsPX

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174826

Add \p{L} into the negated character class instead of a-z, A-Z . \p{L} matches any kind of letter from any language. By adding this to a negated character class would match any character but not of letters.

@"[^\p{L}0-9 -]"

DEMO

string str = "abc // t?% ?? ttt ,. y  Ä Ö Ü ä, ö !";
string result = Regex.Replace(str, @"[^\p{L}0-9 -]", "");
Console.WriteLine(result);
Console.ReadLine();

Output:

abc  t  ttt  y  Ä Ö Ü ä ö 

IDEONE

Upvotes: 7

Related Questions