正则表达式替换无效字符 [英] Regex to replace invalid characters
本文介绍了正则表达式替换无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对RegEx没有太多的经验,所以我使用许多链接的String.Replace()调用来删除不需要的字符-我可以写一个RegEx来简化此操作吗?
I don't have much experience with RegEx so I am using many chained String.Replace() calls to remove unwanted characters -- is there a RegEx I can write to streamline this?
string messyText = GetText();
string cleanText = messyText.Trim()
.ToUpper()
.Replace(",", "")
.Replace(":", "")
.Replace(".", "")
.Replace(";", "")
.Replace("/", "")
.Replace("\\", "")
.Replace("\n", "")
.Replace("\t", "")
.Replace("\r", "")
.Replace(Environment.NewLine, "")
.Replace(" ", "");
谢谢
推荐答案
尝试以下正则表达式:
Regex regex = new Regex(@"[\s,:.;/\\]+");
string cleanText = regex.Replace(messyText, "").ToUpper();
\s
是字符类等效项到 [\t\r\n]
。
如果只想保留字母数字字符,而不是将存在的每个非字母数字字符添加到字符类中,则可以执行以下操作:
If you just want to preserve alphanumeric characters, instead of adding every non-alphanumeric character in existence to the character class, you could do this:
Regex regex = new Regex(@"[\W_]+");
string cleanText = regex.Replace(messyText, "").ToUpper();
其中 \W
是非文字字符(不是 [^ a-zA-Z0-9 _]
)。
Where \W
is any non-word character (not [^a-zA-Z0-9_]
).
这篇关于正则表达式替换无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文