C#Regex没有正确匹配输入字符串

时间:2014-04-09 21:46:02

标签: c# asp.net regex

我正在处理一个ASP.NET表单应用程序,该应用程序从用户输入中获取主课程ID并将其与格式匹配。格式如下:

HIST-1302-233IN-FA2012

或者可能是

XL-HIST-1302-233IN-FA2012

这是我的正则表达式:

string masterCourseRegex = @"(.{4}-.{4}-.{5}-.{6})/|XL-(.{4}-.{4}-.{5}-.{6})";

我已经在Rubular中对此进行了测试,而没有在XL之前进行前向转义,它似乎适用于两种格式。但在我对我的网络应用程序的测试中,代码似乎认为HIST-1302-233IN-FA2012不匹配,因此它遵循代码的路径,表明课程ID与指定的格式不匹配,因此抛出"无效的课程ID格式"当它应该匹配得很好并进入实际使用它的代码时。

我的表单正确识别出前面有XL-并且继续像往常一样处理的事情,我只是在没有XL的情况下遇到标准格式的问题。这是我的代码:

if (!Regex.IsMatch(txtBoxMasterCourse.Text, masterCourseRegex))
                {
                    string msg = string.Empty;
                    StringBuilder sb = new StringBuilder();
                    sb.Append("alert('The course ID " + txtBoxMasterCourse.Text + " did not match the naming standards for Blackboard course IDs. Please be sure to use the correct naming convention as specified on the form in the example.");
                    sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
                    sb.Append("');");
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
                }

我无法看到任何对我来说显而易见的错误,并感谢您的意见。

谢谢!

3 个答案:

答案 0 :(得分:2)

如果我们分解您的表达式并添加一些注释,则更容易看到问题。

string masterCourseRegex = @"
   (    # Capture
    .{4}  # Match any character, exactly four times
    -     # Match a single hyphen/minus
    .{4}  # Match any character, exactly four times
    -     # Match a single hyphen/minus
    .{5}  # Match any character, exacly five times.
    -     # Match a single hyphen/minus
    .{6}  # Match any character, exactly six times
   )    # End Capture
   /    # Match a single forward slash <----------- HERE IS THE PROBLEM
   |    # OR
   XL   # Match the characters XL
   -    # Match a single forward slash
   (
   .{4}   # Match any character, exactly four times
   -      # Match a single hyphen/minus
   .{4}   # Match any character, exactly four times
   -      # Match a single hyphen/minus
   .{5}   # Match any character, exactly five times
   -      # Match a single hyphen/minus
   .{6}   # Match any character, exactly six times
   )"

从原始表达式中删除正斜杠将允许它匹配您的两个示例。

string masterCourseRegex = @"(.{4}-.{4}-.{5}-.{6})|XL-(.{4}-.{4}-.{5}-.{6})";

或者,您可以考虑通过消除.匹配的使用来考虑使表达更具体。例如:

string masterCourseRegex = @"(XL-)?(\w{4}-\d{4}-[\w\d]{5}-[\w\d]{6})";

这也适用于您"HIST-1302-233IN-FA2012""XL-HIST-1302-233IN-FA2012"

的给定示例

在正则表达式中尽可能具体,这通常是一种很好的做法。请记住,.运算符与任何字符匹配,并且它的使用可以使调试正则表达式比它需要的更难。

答案 1 :(得分:1)

不要太明白。尝试类似:

static Regex rx = new Regex( @"
  ^                     # start-of-text
  (XL-)?                # followed by an optional "XL-" prefix
  [A-Z][A-Z][A-Z][A-Z]  # followed by 4 letters
  -                     # followed by a literal hyphen ("-")
  \d\d\d\d              # followed by 4 decimal digits
  -                     # followed by a literal hyphen ("-")
  \d\d\d[A-Z][A-Z]      # followed by 3 decimal digits and 2 letters ("###XX")
  -                     # followed by a literal hyphen
  [A-Z][A-Z]\d\d\d\d    # followed by 2 letters and 4 decimal digits ("NN####")
  $                     # followed by end-of-text
  " , RegexOptions.IgnorePatternWhitespace|RegexOptions.IgnoreCase
  ) ;

你还应该将你的比赛锚定到文本的开头/结尾(除非你愿意接受整个字符串以外的匹配。)

答案 2 :(得分:0)

试试这个:

string masterCourseRegex = @"(XL-)?(\w{4}-\w{4}-\w{5}-\w{6})";