68
votes

I'm completely incapable of regular expressions, and so I need some help with a problem that I think would best be solved by using regular expressions.

I have list of strings in C#:

List<string> lstNames = new List<string>();
lstNames.add("TRA-94:23");
lstNames.add("TRA-42:101");
lstNames.add("TRA-109:AD");

foreach (string n in lstNames) {
  // logic goes here that somehow uses regex to remove all special characters
  string regExp = "NO_IDEA";
  string tmp = Regex.Replace(n, regExp, "");
}

I need to be able to loop over the list and return each item without any special characters. For example, item one would be "TRA9423", item two would be "TRA42101" and item three would be TRA109AD.

Is there a regular expression that can accomplish this for me?

Also, the list contains more than 4000 items, so I need the search and replace to be efficient and quick if possible.

EDIT: I should have specified that any character beside a-z, A-Z and 0-9 is special in my circumstance.

9
4000 is a very small number of items. Why is it so important that the answer is efficient and quick rather than for example readable and maintainable? Have you measured a performance issue here?Mark Byers
@Mark - this is just my ignorance showing, I suppose. 4000 seems to me to be a lot, but I'm apparently wrong. I've done no benchmarks, but the regex seems to be very fast anyway, so I don't think I need to mess around with speeding anything up. I'd actually simplified my example a little, because I didn't want to needlessly convolute my question. I actually have a list of complex class objects, and I'm using the .FindAll with a delegate function to find all matches. The regex comes into play in a property on my class that uses the regex to manipulate a certain string for the delegate function.Jagd
Thank you all for your help! Would that all questions were easy like this for geniuses like yourselves. :DJagd

9 Answers

118
votes

It really depends on your definition of special characters. I find that a whitelist rather than a blacklist is the best approach in most situations:

tmp = Regex.Replace(n, "[^0-9a-zA-Z]+", "");

You should be careful with your current approach because the following two items will be converted to the same string and will therefore be indistinguishable:

"TRA-12:123"
"TRA-121:23"
17
votes

This should do it:

[^a-zA-Z0-9]

Basically it matches all non-alphanumeric characters.

17
votes

[^a-zA-Z0-9] is a character class matches any non-alphanumeric characters.

Alternatively, [^\w\d] does the same thing.

Usage:

string regExp = "[^\w\d]";
string tmp = Regex.Replace(n, regExp, "");
9
votes

You can use:

string regExp = "\\W";

This is equivalent to Daniel's "[^a-zA-Z0-9]"

\W matches any nonword character. Equivalent to the Unicode categories [^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}].

3
votes

Depending on your definition of "special character", I think "[^a-zA-Z0-9]" would probably do the trick. That would find anything that is not a small letter, a capital letter, or a digit.

2
votes
tmp = Regex.Replace(n, @"\W+", "");

\w matches letters, digits, and underscores, \W is the negated version.

2
votes

For my purposes I wanted all English ASCII chars, so this worked.

html = Regex.Replace(html, "[^\x00-\x80]+", "")
0
votes

If you don't want to use Regex then another option is to use

char.IsLetterOrDigit

You can use this to loop through each char of the string and only return if true.

0
votes
public static string Letters(this string input)
{
    return string.Concat(input.Where(x => char.IsLetter(x) && !char.IsSymbol(x) && !char.IsWhiteSpace(x)));
}