Search code examples
c#regexstringapostrophepunctuation

Allow specific punctuation and remove aphostrophe


I'm trying to remove specific punctuation from string

I need to keep in string only ".", ",", "!", "+", "*", "-" but remove everything, including apostrophe "'" for example

what's up!

must be:

whats up!

So this way, it removes everything

string res = Regex.Replace(filtr2, @"[^\w\s]", "");  

Solution

  • Hope that you are looking for something like this :

    string rexFormat=@"[^0-9a-zA-Z\.,!+\-* \w ]";
    string myInPutString ="what'sup ! my dear friend?";
    string replacedString = Regex.Replace(myInPutString, rexFormat, string.Empty);
    // will give you whatsup ! my dear friend
    

    The special characters ' and ? were removed