简体   繁体   English

C#什么是特殊字符的正则表达式

[英]C# what is the regex expression for Special characters

What is the regex expression for the special characters that is greater than 128 in the ASCII extended table? ASCII扩展表中大于128的特殊字符的正则表达式是什么?

I have a line with special characters like below, and every special char should be replaced with a space. 我在一行中包含特殊字符,如下所示,每个特殊字符都应替换为一个空格。

input --->  "H€ELLOŠŠŠŠWorld$"
output -->  "H ELLO    World$"

NB: $ is special character that has ASCII<128 注意:$是具有ASCII <128的特殊字符

To know ASCII<128 characters http://www.ascii-code.com/ 要了解ASCII <128个字符的http://www.ascii-code.com/

Please try the following: 请尝试以下操作:

var re = new Regex(@"[\u0080-\uFFFF]");

var s = re.Replace("H€ELLOŠŠŠŠWorld. This is a sample 1234 $.", " ");

Console.WriteLine(s);

OUTPUT OUTPUT

H ELLO    World. This is a sample 1234 $.

IDEONE DEMO IDEONE演示

Why bother with regexes for this task? 为什么要为正则表达式打扰呢?

var str = "H€ELLOŠŠŠŠWorld$";

var sb = new StringBuilder(str.Length);
foreach(var c in str)
    sb.Append(c <= 128 ? c : ' ');

var result = sb.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM