Replace [^a-zA-Z0-9] with an empty string. string str = “This-is-my+++***RegexPattern&Text”; Regex rgx = new Regex(“[^a-zA-Z0-9]”); str = rgx.Replace(str, “”); OUTPUT: ThisismyRegexPatternText If you want to add an exception then you should add this character like this to the regex pattern (let’s assume you wish to exclude the ampersand): [^a-zA-Z0-9 &] string str = “This-is-my+++***RegexPattern&Text”; Regex rgx = […]
Tag: regex
How to trim whitespaces between characters in C#
trim() function does NOT remove “all whitespace characters” of your string. So instead of trim(), you can use String.Replace method: string str = “C Sharp”; str = str.Replace(” “, “”); OUTPUT: CSharp or if you want to remove all whitespace characters (space, tabs, line breaks…) string str = “C Sharp”; str = Regex.Replace(str, @”\s”, “”); […]