正则表达式删除特殊字符 [英] Regex remove special characters

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

问题描述

我们需要一个C#函数,该函数将从字符串中删除所有特殊字符.

We need a C# function which will remove all special characters from a string.

还可以将乔治的"更改为乔治"(删除单引号和字符s)吗?

Also, is it possible to change "George's" to "George" (remove both single quote and character s)?

推荐答案

此方法将删除字母,数字和空格以外的所有内容.还将删除任何'或后跟字符s.

This method will removed everything but letters, numbers and spaces. It will also remove any ' or " followed by the character s.

public static string RemoveSpecialCharacters(string input)
{
    Regex r = new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
    return r.Replace(input, String.Empty);
}

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

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