views:

60

answers:

1

Hello,
I am using regular expressions to validate user input. The following code collects a matches accessible with theMatch.Groups["identifier"]. How can i get a list of sub-strings that did not match in each group?

#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;


namespace RegExGroup
{

   class Test
   {
      public static void Main( )
      {
         string string1 = "04:03:27 127.0.0.0 comcom.com";

         // group time = one or more digits or colons followed by space
         Regex theReg = new Regex( @"(?<time>(\d|\:)+)\s" +
         // ip address = one or more digits or dots followed by  space
         @"(?<ip>(\d|\.)+)\s" +
         // site = one or more characters
         @"(?<site>\S+)" );

         // get the collection of matches
         MatchCollection theMatches = theReg.Matches( string1 );

         // iterate through the collection
         foreach ( Match theMatch in theMatches )
         {
           if ( theMatch.Length != 0 )
           {
            Console.WriteLine( "\ntheMatch: {0}",
               theMatch.ToString( ) );
            Console.WriteLine( "time: {0}",
               theMatch.Groups["time"] );
            Console.WriteLine( "ip: {0}",
               theMatch.Groups["ip"] );
            Console.WriteLine( "site: {0}",
               theMatch.Groups["site"] );
           }
         }
      }
   }
}

so if user inputs 0xx:03:27 127.0.0.0 ?.com
I want to output

 time:  0xx:03:27
 site:  ?.com

Also, anyone have good references for using regexs in C#?
Thanks, any help appreciated.

+1  A: 

Are you asking how to determine which specific capture group failed to match? To my knowledge once the match fails you won't be able to extract such information. If it fails it fails; no partial matching attempt info can be retrieved. What you could do is apply the entire regex as is to check the pattern in the desired order. Then, if it fails, try each part of your regex separately and tell the user which one failed (time, ip, site). This approach may make sense in this scenario, but might not work for all types of patterns.

Regarding references, here are a few links:

If you're looking for a good book then the most popular is Jeffrey Friedl's Mastering Regular Expressions. A recent book that has positive ratings is the Regular Expressions Cookbook.

Ahmad Mageed
Thanks for the pointers. I split the string and validated each substring separately. I don't see why i need to use MatchCollection since i cannot do the trick in one step.
Slabo