in reply to Re: Quicker way to do this IF statement 1-5
in thread Quicker way to do this IF statement 1-5

The correct regex solution :)
/^[1-5]\z/

Replies are listed 'Best First'.
Re^3: Quicker way to do this IF statement 1-5
by graff (Chancellor) on May 20, 2007 at 18:52 UTC
    To say that using "\z" is "the correct regex solution" (and imply that using "$" is "incorrect") is a bit silly, esp. in the absence of more detailed info regarding the application.

    There are some situations where the two solutions will have different behaviors, and in such cases, it's up to the programmer to decide which is more appropriate. Either one could be more useful (or "correct") than the other, depending on the task. But in many common situations, they behave the same, and neither is "more correct" than the other.

      To say that using "\z" is "the correct regex solution" (and imply that using "$" is "incorrect") is a bit silly, esp. in the absence of more detailed info regarding the application.

      If, as the OP requested, the variable must contain an integer between 1 and 5 inclusive, "1\n" seems like an invalid option. Unfortunately, /^\d$/ gives a false positive for that value.

      But in many common situations, they behave the same...

      I can only think of one case, and that's where the string does not have a trailing newline.