CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Validation Code

Threaded View

  1. #2
    Join Date
    Dec 2011
    Posts
    61

    Re: Validation Code

    If you feed the text from a file, then it's sth. like this:

    Code:
                Regex regex = new Regex(@"^(\s)*(\d ){6}\d(\s)*$");//note there's a blank space after \d
                bool isValid = true;
                using (StreamReader reader = new StreamReader(File.Open("test.txt", FileMode.Open)))
                {
                    while (reader.Peek() > -1)
                    {
                        if (regex.IsMatch(reader.ReadLine()) == false)
                        {
                            isValid = false;
                            break;
                        }
                    }
                    if (isValid)
                    {//do sth. ...
                     }
                 }
    If you already assign the text to a string variable, i.e., sth. like this:
    string t = "1 2 3 4 5 6 7\r\n2 4 4 6 7 8 9";
    then use string[] ts = t.Split(new string[]{"\r\n"});
    to split the string into the array, then use a loop and the above regex to test if each array element matches

    This works if you allow "jawed" text, like this:
    Code:
      1 2 3 4 5 6 7
    2 5 3 6 3 6 4
         3 5 6 7 8 9 9
    If you don't allow this and want the collumns to strictly align, you need to record the number of white space preceding the first line, and check if the following lines have equal number of preceding white space.
    Last edited by Silent Sojourner; January 4th, 2012 at 04:45 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured