Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

asp.net - Why order matters in this RegEx with alternation?

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

  1. A sequence of numbers.
  2. Literal string 'Number of rooms'.
  3. 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" />
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...