Why order interlace questions in this RegEx?

The requirements for the TextBox control should have the following valid values:

  • Sequence of numbers.
  • Literal line 'Number of rooms'.
  • No value at all (left blank). Without specifying a value at all, it should be allowed to pass a RegularExpressionValidator.

After RegEx gave the desired results (successfully confirmed 3 types of inputs):

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

However, I could not come up with an explanation when a colleague asked why the following could not be verified when the line “Number of rooms” was indicated (requirement No. 2):

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

An explanation of why the alternation order is important in this case would be very insightful.

UPDATE:

" " , . aspx , " ". aspx:

<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" />
+5
3

, , Regex .

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

" ". , .

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

. . " "

|| #. , .

Update: . - RegularExpressionValidator, , .

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

, , - . .

+7

, [0-9]* , .
, , :

^[0-9]*$|Number of rooms

^ $, , , ​​ " ", .
, , , ...

+2

, regex Number of rooms|[0-9]+ [0-9]+|Number of rooms, pattern [0-9]* ( ) (* {0,}, " ..." ).

+2

All Articles