[英]C# Regex doesn't correctly match the input string
我正在使用一个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之前的前向转义符,它似乎适用于两种格式。 但是在测试我的Web应用程序时,该代码似乎认为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);
}
我看不到任何对我来说显而易见的错误,并感谢您的投入。
谢谢!
如果我们分解您的表达方式并添加一些评论,则更容易发现问题。
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"
。
通常,在正则表达式中尽可能具体是一种很好的做法。 记住那个.
运算符可以匹配任何字符,并且使用它会使调试正则表达式变得更加困难。
别幻想。 尝试类似:
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
) ;
您还应该将匹配项锚定到文本的开头/结尾(除非您愿意接受整个字符串以外的匹配项)。
试试这个:
string masterCourseRegex = @"(XL-)?(\w{4}-\w{4}-\w{5}-\w{6})";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.