Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何显示未匹配的子正则表达式?_C#_Regex_Validation - Fatal编程技术网

C# 如何显示未匹配的子正则表达式?

C# 如何显示未匹配的子正则表达式?,c#,regex,validation,C#,Regex,Validation,我正在使用正则表达式验证用户输入。以下代码收集Match.Groups[“identifier”]可访问的匹配项。如何获得每组中不匹配的子字符串列表 #region Using directives using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace RegExGroup { class Test {

我正在使用正则表达式验证用户输入。以下代码收集Match.Groups[“identifier”]可访问的匹配项。如何获得每组中不匹配的子字符串列表

#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"] );
           }
         }
      }
   }
}
此外,任何人都有在C#?

谢谢,非常感谢您的帮助。

您是否在询问如何确定哪个特定捕获组未匹配?据我所知,一旦比赛失败,你将无法提取此类信息。如果失败了,它就失败了;无法检索部分匹配尝试信息。您可以做的是应用整个正则表达式,以便按照所需的顺序检查模式。然后,如果失败,请分别尝试正则表达式的每个部分,并告诉用户哪个部分失败(时间、ip、站点)。这种方法在这种情况下可能有意义,但可能不适用于所有类型的模式

关于参考资料,这里有几个链接:

  • -不错的教程,Expresso工具的作者
  • -在中测试正则表达式的好工具,包括一些示例和上面的教程。有关其他工具,请查看上面的MSDN链接
  • -列出了几个正则表达式链接

如果你在寻找一本好书,那么最受欢迎的是杰弗里·弗里德尔的。最近一本获得正面评价的书是。

谢谢你的指点。我拆分字符串并分别验证每个子字符串。我不明白为什么我需要使用MatchCollection,因为我不能一步到位。快速简单的Perl方法是
(?{print$^N,“\N})
 time:  0xx:03:27
 site:  ?.com