umarali
umarali

Reputation: 59

Can someone give me a regular expression for a number to be between 1 and 5

Can someone give me a regular expression for a number to be between 1 and 5, single digit

e.g. input has to be a number between 1 and 5 , 55 or 23 would not match

Upvotes: 4

Views: 10992

Answers (4)

pauljwilliams
pauljwilliams

Reputation: 19225

Much more concise to say

if(x> 0 and x<5)

IMHO

Upvotes: 0

Spudley
Spudley

Reputation: 168753

Would it not be simpler to check it as a number (ie if(x>=1 && x<=5) or something similar) rather than using a regex?

Upvotes: 1

Faisal Feroz
Faisal Feroz

Reputation: 12785

Maybe this should do [1-5]

Upvotes: -3

Mark Byers
Mark Byers

Reputation: 838786

Try using anchors:

/^[1-5]$/

Explanation:

^     Start of line/string.
[1-5] A digit between 1 and 5.
$     End of line/string.

Upvotes: 15

Related Questions