Click here to Skip to main content
11,578,739 members (62,987 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB .NET regular-expression , +
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 26-Nov-11 1:52am
Comments
ProEnggSoft at 29-Mar-12 4:10am
   
Please see my solution (3). It may be helpful.
ProEnggSoft at 29-Mar-12 4:11am
   
Good question. +5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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/[^]
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

you can use this expression
expression="*[1-6][9][22-28][31,38]$";
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

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

  Print Answers RSS
0 Peter Leow 135
1 Frankie-C 115
2 OriginalGriff 113
3 Richard MacCutchan 85
4 Afzaal Ahmad Zeeshan 75
0 OriginalGriff 933
1 Sergey Alexandrovich Kryukov 740
2 Abhinav S 573
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 403


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 29 Mar 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100