为什么要为了在这个正则表达式与交替的问题?在这个、要为、问题、正则表达式

2023-09-03 01:43:21 作者:终究无缘了

有一个TextBox控件要求要接受以下为有效输入:

数字序列。 在文字字符串的房间数。 在所有没有值(左空白)。不指定的值都应该允许RegularEx pressionValidator通过。

随着正则表达式,得到所希望的结果(成功验证了3种类型的输入):

 房间数| [0-9] *
 

不过,我不能拿出一个解释,当一个同事问为什么以下无法验证指定的字符串的房间数时(要求#2):

 [0-9] * |房间数
 
请问这个正则表达式的意思是什么,谢谢

这是解释为什么交替事项在这种情况下的顺序将是非常有见地确实。

更新:

第二个正则表达式成功地在控制台应用程序匹配目标字符串房间数,如图这里。然而,使用相同的前pression在ASPX标记当输入是房数不匹配。以下是有关ASPX标记:

 < ASP:文本框=服务器ID =TextBox1的>
< / ASP:文本框>

< ASP:RegularEx pressionValidator ID =RegularEx pressionValidator1
EnableClientScript =假=服务器的ControlToValidate =TextBox1的
ValidationEx pression =[0-9] * |房间数
的ErrorMessage =RegularEx pressionValidator>< / ASP:RegularEx pressionValidator>

< ASP:按钮的ID =Button1的=服务器文本=按钮/>
 

解决方案

订单的问题,因为这是为了它的正则表达式引擎将尝试匹配。

案例1:房间号码| [0-9] *

在这种情况下,正则表达式引擎将首先尝试匹配文房号。如果失败将尝试匹配数字或没有。

案例2: [0-9] * |客房数量

在这种情况下,发动机会先尝试匹配数量还是一无所获。但是,什么都不会的总是的匹配。在这种情况下,它永远不需要尝试房数

这是一种像||运营商在C#。一旦左侧匹配右侧被忽略。

更新: 要回答你的第二个问题。它的行为不同的RegularEx pressionValidator,因为这是做不仅仅是检查匹配。

  // .....
匹配M = Regex.Match(controlValue,ValidationEx pression);
返程(m.Success&安培;&安培; m.Index == 0安培;&安培; m.Length == controlValue.Length);
// .....
 

据进行匹配检测,以​​及确保比赛的长度是整个字符串。这排除了部分或空的匹配。

Requirements for a TextBox control were to accept the following as valid inputs:

A sequence of numbers. Literal string 'Number of rooms'. No value at all (left blank). Not specifying a value at all should allow for the RegularExpressionValidator to pass.

Following RegEx yielded the desired results (successfully validated the 3 types of inputs):

"Number of rooms|[0-9]*"

However, I couldn't come up with an explanation when a colleague asked why the following fails to validate when the string 'Number of rooms' is specified (requirement #2):

"[0-9]*|Number of rooms"

An explanation as to why the ordering of alternation matters in this case would be very insightful indeed.

UPDATE:

The second regex successfully matches the target string "Number of rooms" in console app as shown here. However, using the identical expression in aspx markup doesn't match when the input is "Number of rooms". Here's the relevant aspx markup:

<asp:TextBox runat="server" ID="textbox1" >
</asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
EnableClientScript="false" runat="server" ControlToValidate="textbox1" 
ValidationExpression="[0-9]*|Number of rooms" 
ErrorMessage="RegularExpressionValidator"></asp:RegularExpressionValidator>

<asp:Button ID="Button1" runat="server" Text="Button" />

解决方案

The order matters since that is the order which the Regex engine will try to match.

Case 1: Number of rooms|[0-9]*

In this case the regex engine will first try to match the text "Number of room". If this fails will then try to match numbers or nothing.

Case 2: [0-9]*|Number of rooms:

In this case the engine will first try to match number or nothing. But nothing will always match. In this case it never needs to try "Number of rooms"

This is kind of like the || operator in C#. Once the left side matches the right side is ignored.

Update: To answer your second question. It behaves differently with the RegularExpressionValidator because that is doing more than just checking for a match.

// .....
Match m = Regex.Match(controlValue, ValidationExpression);
return(m.Success && m.Index == 0 && m.Length == controlValue.Length); 
// .....

It is checking for a match as well as making sure the length of the match is the whole string. This rules out partial or empty matches.