我正在尝试删除要使用 Playfair Cipher 加密的字符串中的所有标点符号和空格。我不明白为什么这条线不起作用。
s = Regex.Replace(s, @"[^\w\s]", string.Empty);
    我正在尝试删除要使用 Playfair Cipher 加密的字符串中的所有标点符号和空格。我不明白为什么这条线不起作用。
s = Regex.Replace(s, @"[^\w\s]", string.Empty);
    The [^\w\s] means remove anything that's not a word or whitespace character. 
Try this instead:
s = Regex.Replace(s, @"[^\w]", string.Empty);
You could also use:
s = Regex.Replace(s, @"\W", string.Empty);
Of course that will leave underscores as those are considered word characters. To remove those as well, try this:
s = Regex.Replace(s, @"[\W_]", string.Empty);
Or this:
s = Regex.Replace(s, @"\W|_", string.Empty);
    使用 Linq 代替 Regex 怎么样?
string str = "abc; .d";
var newstr = String.Join("", str.Where(char.IsLetterOrDigit));
    ^字符意味着不是。我用[^A-Za-z0-9-]连字符代替所有不是字母数字的东西。
你最好的选择可能是使用[^A-Za-z]contains \w,_我0-9猜你不想保留它。
以下正则表达式将删除任何 nota-z或A-Z.
s = Regex.Replace(s, @"[^A-Za-z]", string.Empty);