c# - "00000000000000000000000000000" matches Regex "^[1-9]|0$" -


in .net4.5, find result of

system.text.regularexpressions.regex.ismatch(     "00000000000000000000000000000", "^[1-9]|0$") 

is true.

the result expect false. don't know why. can me?

update: in beginning, validating regular expression ^-?[1-9]\d*|0$ used match integer found on internet , find string multiple 0 matches regular expression.

the issue alternator's binding behavior. default (i.e. without using grouping), expression containing alternator (|) match either value left of alternator, or value right.

so in expression, you're matching either 1 of these:

  • ^[1-9]
  • 0$

your call ismatch method returns true because second of 2 option matches string 00000000000000000000000000000.

to restrict alternator's binding specific part of expression, need group using parentheses, follows:

^([1-9]|0)$ 

putting together, strict expression validate integers, disallowing leading zeroes , negative zero, this:

^(-?[1-9][0-9]*|0)$ 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -