从字符串中删除除空格以外的特殊字符 [英] Remove special characters from a string except whitespace

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

问题描述

我正在寻找一个正则表达式来从字符串中删除除空格之外的所有特殊字符.并且可能用一个空格替换所有多个空格.

I am looking for a regular expression to remove all special characters from a string, except whitespace. And maybe replace all multi- whitespaces with a single whitespace.

例如"[one@ !twothree-four]" 应该变成"一二三四"

我尝试使用 str = Regex.Replace(strTemp, "^[-_,A-Za-z0-9]$", "").Trim() 但它不起作用.我还尝试了更多,但它们要么去掉空格,要么不替换所有特殊字符.

I tried using str = Regex.Replace(strTemp, "^[-_,A-Za-z0-9]$", "").Trim() but it does not work. I also tried few more but they either get rid of the whitespace or do not replace all the special characters.

推荐答案

[ ](?=[ ])|[^-_,A-Za-z0-9 ]+

试试这个.看演示.替换为空字符串.看演示.

Try this.See demo.Replace by empty string.See demo.

http://regex101.com/r/lZ5mN8/69

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

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