Tuesday, February 10, 2009

.Net Regular Expression for Numbers Range

At work, a colleague asked me for help with with a regular expression he was trying to get working. It was simple and quick and worth a post in my blog.

He wanted to validate for numbers between 1 and 50. After a couple of minutes of testing at http://www.regextester.com/, I sent him this:

Dim re As New Regex("^([0-9]|[0-4][0-9]|[5][0])$")

MsgBox(re.IsMatch(“123”))
MsgBox(re.IsMatch(“2”))
MsgBox(re.IsMatch(“50”))
MsgBox(re.IsMatch(“51”))