locked
using regular expressions RRS feed

  • Question

  • Hello, I have an file that I am reading and it has some goffy characters in it and I want to use a regular expression to clean up those characters. I am not  sure how to construct a regular expression (because i haven't use the regular expressions this is my first time I use them) which find these characters and replace them with ""non spaces . for example some are non printable characters like a square that is the one that appear mostly....

    Thanks !!!!!

    "Everything is worthless if you don't give yourself a chance to try" by me... ;P
    Monday, December 21, 2009 7:01 PM

Answers

  • Regex is about knowing your data. If it were me, I would determine exactly what that was (e.g, linefeed, carriage return, etc...) and then appropriate a solution.

    Try something like this.

    String inputText = Regex.Replace(InputText.Text, "[\x00-\x1f]", "");
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Elsanti Monday, December 21, 2009 8:43 PM
    Monday, December 21, 2009 8:09 PM

All replies

  • The pattern [\x00-\x1f] matches all control characters.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Monday, December 21, 2009 7:05 PM
  • Show us what characters you need replaced...

    http://msdn.microsoft.com/en-us/library/218s85f8%28VS.80%29.aspx
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Monday, December 21, 2009 7:06 PM
  • thanks for the reply, this is the character  :      I don't know if it can be show....


    "Everything is worthless if you don't give yourself a chance to try" by me... ;P
    Monday, December 21, 2009 7:25 PM
  • Those chars probably are line feeds, tabs or some other control chars.

     The \s matches any white space character and the \S matches any non-white space character.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Monday, December 21, 2009 7:31 PM
  • thanks john so the regular expression willl be something like this:

    Regex val = new Regex("[\x00-\x1f]");
                //val.IsMatch(InputText.Text);
                val.Replace(InputText.Text);
    ????? 

    Thanks for the help so far!!!
    "Everything is worthless if you don't give yourself a chance to try" by me... ;P
    Monday, December 21, 2009 8:04 PM
  • Regex is about knowing your data. If it were me, I would determine exactly what that was (e.g, linefeed, carriage return, etc...) and then appropriate a solution.

    Try something like this.

    String inputText = Regex.Replace(InputText.Text, "[\x00-\x1f]", "");
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Elsanti Monday, December 21, 2009 8:43 PM
    Monday, December 21, 2009 8:09 PM
  • thanks a lot john!!! yea i think it is a carriage return, that mostly in the last line generate the square....
    Thanks again!!!!

    "Everything is worthless if you don't give yourself a chance to try" by me... ;P
    • Marked as answer by Elsanti Monday, December 21, 2009 8:43 PM
    • Unmarked as answer by Elsanti Monday, December 21, 2009 8:43 PM
    Monday, December 21, 2009 8:42 PM