Very Simple Regular Expressions (Starting with Numbers)


If you want to validate strings, text, or whatever it may be, validating with Regular Expression is an easy way to determine that the data is valid. Whether it’s an application or a web-site, you can validate users’ input to ensure it has met all requirements before continuing, or validate your own data.


This small example checks whether the data is simply ‘123’, anything else will fail. We use the ‘^’ symbol to show where the beginning of the data is, and the ‘$’ symbol to show where the end of the data is.


The example above will test the data to ensure it is a single number between 0 to 9. ‘0’, ‘1’, ‘2’, ‘3’, and ‘9’ are examples of data that will pass the above regular expression. ‘a’, ’10’, and ‘ 1’ are all examples of data that will fail.

So what if we wanted 2 numbers?


The example above will match 0 to 9 or 00 to 99. The ‘|’ symbol simply specifies ‘or’ so in affect we have [0-9] for 0 to 9 | [0-9][0-9] for 00 to 99. Because we are using the ‘|’ symbol, we need to wrap brackets around our statement to show where the ‘or’ statement begins and where it ends.

Now lets work with time.


12 hour format is pretty damn simple now that you know the basics. The first part ‘1[0-2]’ means 1 followed by 0 to 2 (for example 10, 11, 12), ‘or’ ‘(0*[1-9])’ for 1 to 9 or 01 to 09. The ‘*’ means whatever character before it is optional, so the ‘0’ is optional but can only occur once if it was there.

So this will cover 1 to 9, 01 to 09, and 10, 11, 12. The ‘:’ symbol is exactly that, and the last two parts ‘[0-5][0-9]’ means 00 to 59. ‘1:15’, ‘9:59′, and ’12:39′ will all pass, but ’00:38′, ’13:32’, and ‘3:83’ will all fail.

I’m on a quest to make the ultimate regular expression for date and time, but there’s no use giving you the whole thing without first telling you how to get there. Keep an eye on my blog for more posts to come.

I must also note that the above and future examples are Perl-derivative regular expressions. Java, JavaScript, PCRE, Python, Ruby, Microsoft’s .NET Framework, and the W3C’s XML Schema all use regular expression syntax similar to Perl’s. Some languages and tools such as PHP support multiple regular expression flavors.

* RegExp – Mozilla Developer Center
* Regular Expression – Wikipedia


Related Articles