Got more questions? Find advice on: ASP | SQL | XML | Windows
Welcome to RegexAdvice Sign in | Join | Help

Remove \W while ignoring White Space

  •  07-29-2010, 3:46 AM

    Remove \W while ignoring White Space

    Hi Folks.

    I am not to fermilliar with Reg X and i am tring to remove all none Alpa/Numeric Chars while leaving white spaces.  In c#

    Here is my code.

            string searchString = Regex.Replace(search.Text.Trim().ToLower(), @"[\W]", "");
                Response.Redirect(searchString.Replace(" ", "-") + "-search.htm");

    I need to put the - Char where there is a space.  So ive looked around a bit and i am still stumped.  I get that [\s] is all white spaces, but i do not get how to make this an exception to the rule [\W].

    I did try  string searchString = Regex.Replace(search.Text.Trim().ToLower(), @"[!\s]|[\W]", "");  This was ok but then !"$£ style Chars came through.

    And also i tryed string searchString = Regex.Replace(search.Text.Trim().ToLower(), @"[\W]", "", RegexOptions.IgnorePatternWhitespace) but this was wrong as well.

    Help....

    Thanks Panda.

    PS.  What dose the  @ symbol mean at the start of the pattern,   lots of examples ommited this, can i?

    Filed under: ,
View Complete Thread