正则表达式,仅从字符串中删除特定的特殊字符 [英] Regex for removing only specific special characters from string

查看:2040
本文介绍了正则表达式,仅从字符串中删除特定的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个正则表达式,它将在以下基础上删除特殊字符:

I'd like to write a regex that would remove the special characters on following basis:


  • 要删除空格字符

  • @ & '< >

  • To remove white space character
  • @, &, ', (, ), <, > or #

我写了这个正则表达式,可以成功删除空格:

I have written this regex which removes whitespaces successfully:

 string username = Regex.Replace(_username, @"\s+", "");

但是我想升级/更改它,以便它可以删除上面提到的字符。

But I'd like to upgrade/change it so that it can remove the characters above that I mentioned.

有人可以帮我吗?

推荐答案

 string username = Regex.Replace(_username, @"(\s+|@|&|'|\(|\)|<|>|#)", "");

这篇关于正则表达式,仅从字符串中删除特定的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆