locked
Regex RRS feed

  • Question

  • User1308732545 posted

    Hi All, 

    what is the regex for removing all these below in a string 

    1. Spaces (on,two or many)

    2. underscore,

    3. hypen

    4. other special characters like  * & ' "" . , \ /

    Thanks 

    Tuesday, July 15, 2014 6:48 AM

Answers

  • User2103319870 posted

    You can try with the below code

    String strValue = "This_is_a.t*e&s't.,s\t/ri n  g";
    		//Remove Space,Hypen,Undescrore,* & ' "" . , \ / from the string
    		strValue = Regex.Replace(strValue, @"\s|\-|_|\*|&|\'|""|\.|,|\\|/|", "");
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 16, 2014 12:16 AM
  • User1308732545 posted

    Hi , 

    thanks for the answers ..

    Those regex's worked ..

    In addition I found one more regex :  "[\W_]+"..

    even this works fine ...

    Thanks 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 16, 2014 6:39 AM

All replies

  • User-1516073966 posted

    Hi

    Checkout the following and test with few inputs.

    string strValue = @"This_*-    is\/  .,'some&   text with special characters and white space";
    		
    Regex ex = new Regex(@"[-\\/_*.,&'\s+]");				
    string result = ex.Replace(strValue, @" ");
    Console.WriteLine(Regex.Replace(result, @"\s+", " "));

    Tuesday, July 15, 2014 7:28 AM
  • User2103319870 posted

    You can try with the below code

    String strValue = "This_is_a.t*e&s't.,s\t/ri n  g";
    		//Remove Space,Hypen,Undescrore,* & ' "" . , \ / from the string
    		strValue = Regex.Replace(strValue, @"\s|\-|_|\*|&|\'|""|\.|,|\\|/|", "");
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 16, 2014 12:16 AM
  • User1308732545 posted

    Hi , 

    thanks for the answers ..

    Those regex's worked ..

    In addition I found one more regex :  "[\W_]+"..

    even this works fine ...

    Thanks 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 16, 2014 6:39 AM