Tuesday, February 14, 2012

Regular Expression for printable page ranges.

Last week we got a  requirement to enable printing for our student portal which was developed using Adobe Flex. And all the documents of the portal is on Flex. So our approach was to use Flex Printing.

Our print dialog box has a text box where the user can enter the range of pages to be printed (just like all other applications which has printing ;) ). I was searching for a regular expression which validates the page range pattern. While sufring web I found a cool app where you can write the regular expression and write different test cases agains that. And the interesting thing is it has been written in Flex. So if you are using Flex then you don't have to worry about platform specific stuff. (If the expression is correct in this app then it should obviously work in your Flex application.

And there is a good collection of pre-defined regular expressions which are usable in many cases. I could find one for my requirement as well. But since I needed further more restrictions I had to came up with my own one.


This is my solution :) 
/^([1-9][0-9]{0,2}|[1-9][0-9]{0,2}-[1-9][0-9]{0,2})(,([1-9][0-9]{0,2}|[1-9][0-9]{0,2}-[1-9][0-9]{0,2}))*$/
which validates a pattern like 1,2-5,6,7,12-15

You can access the aforementioned test bed using the url http://ryanswanson.com/regexp/#start


No comments:

Post a Comment