Click here to Skip to main content
15,891,902 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have text box for Question Complexity,and
Valid Cases :
Case #01 : 1,2,5,7,9
Case #02 : 3-5
Case #03 : 1,2,5-7

In Valid Case 01 (bold): 1,4,5-7-8
In Valid Case 02 : 2-4-6-8, Correct way is 2-8

Here numbers are complexities of question.The algorithm will generate question according to selected question complexity.

For example in Case #02 ( all questions with complexity of 3,4 and 5 will be selected)
and
For Case #03 ( all question with complexity 1,2 and 5,6,7 will be selected)

my Tested regular expression after lots of testing is
\d+(,\d|(-\d+)(,\d+))* 


but there is a problem
it is solving the problem and handling ( ex 2-5-7 ) but it requires (,digit) in the end aswell.


for testing regular expression online you can visit : regular expression online testing

Please can some one help me in this. i need to validate complexity like Microsoft Word Printing options.
Or any link or reference will be helpful. Thanks
Posted
Updated 26-Jul-12 6:58am
v2
Comments
Kenneth Haugland 26-Jul-12 13:05pm    
Whats the problem with \d+\-\d+ ?

^(\d+|\d+-\d+)(,(\d+|\d+-\d+))*$
Seems to work. But remember, this would also allow for a range like "100-3", which you may not want, so you might need some extra error checking in your code.
 
Share this answer
 
Comments
Anwar Mehmood 26-Jul-12 13:50pm    
Thanks a lot, it was helpful and indeed i will apply logic to test 100-3 condition.
How about a named capture group:
C#
(?<many>\d+(\-\d+)+)|(?<one>(?<=^|,)\d+(?=$|,))


Its basic usage is this:

VB
Private Function GetPages(ByVal input As String) As List(of Integer)
    Dim output as New List(of Integer)
    Dim Number As String = "(?<many>\d+(\-\d+)+)|(?<one>(?<=^|,)\d+(?=$|,))"

    For Each Match As Match In Regex.Matches(input, Number)
       
        If Not Match.Groups("many").Value = String.Empty Then
                'Her you will have a span of several pages 3-6
                dim temp() as string = Match.Groups("many").Value.ToString.Split("-")
                For i as integer = temp(0) to temp(temp.count-1)
                    output.add(i)
                next
        ElseIf not Match.Groups("one").Value = String.Empty Then
               Output.Add(Cint(Match.Groups("one").Value.ToString))
        End If
    Next

    Return output
End Function


You can see more fanzy stuff with regex here:
Evaluate Complex and Real Math Calculator[^]
 
Share this answer
 
v6
Comments
Anwar Mehmood 26-Jul-12 13:49pm    
Thankful to you for time and answer, can you explain a bit about named capture group!
Kenneth Haugland 26-Jul-12 14:09pm    
By updating my answer I hope you understand it better :) If not then give me a reply.

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