Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
I have a problem writing my pattern for printer dialog. It must be like below:
Pages: 1-6,9,22-28,31,38

It means pages 1 to 6 and 9 and pages from 22 to 28 and pages 31 and 38
How can I match this expression?
Thanks in advance.
Posted
Comments
ProEnggSoft 29-Mar-12 4:10am    
Please see my solution (3). It may be helpful.
ProEnggSoft 29-Mar-12 4:11am    
Good question. +5

 
Share this answer
 
As seen from the question the pattern to be matched is

1. Individual page number like 9,31
2. Page range like 1-6,22-28
Individual page numbers and Page range are separated by , in any order.

For this purpose the following regular expression can be used

^\s*((\d+|\d+\s*-\s*\d+)\s*,\s*)*(\d+|\d+\s*-\s*\d+)\s*$

The first part of expression ^\s*((\d+|\d+\s*-\s*\d+)\s*,\s*)* matches either a single page number or a page range (- surrounded by zero or more spaces) followed by a , (surrounded by zero or more spaces)
then the second part of expression ensures that string ends with either single page number or a page range with out a comma at the end

(\d+|\d+\s*-\s*\d+)\s*$

This can be tested online at
http://regexhero.net/tester/[^]
 
Share this answer
 
v3
you can use this expression
expression="*[1-6][9][22-28][31,38]$";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900