C#Regex没有';t与输入字符串正确匹配
本文关键字:字符串 输入 没有 C#Regex | 更新日期: 2023-09-27 17:57:43
我正在开发一个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})";