Got more questions? Find advice on: ASP | SQL | XML | Windows
Welcome to RegexAdvice Sign in | Join | Help

Regular Expression that matches a string containing a delimited list of digits

  •  08-29-2007, 3:25 PM

    Regular Expression that matches a string containing a delimited list of digits

    I need a regex that matches a string containing a delimited list of digits.  While this seems fairly straightforward, the following requirements must be met:

    1)  Any number of spaces that occur before or after a delimiter should be accepted (e.g.  '3   ,   9   ' is OK)
    2)  The string can end with spaces or a delimiter (e.g. '3  , 9 ,  8  ,' and '3  , 9 ,  8   ' is OK)
    3)  The string can not contain any other characters other than a digit or delimiter (e.g. 3  , 9 , a , 8' is not OK)
    4)  The delimiter can be either a comma (,) or semi-colon (;)
    5)  There should be a Group Or Match (not a combination of each though) created for each token found
    6)  It must work in .Net 2.0

    I have developed the following expression which works for every requirements I have except #4. 

    Expression
    ----------
    ^([0-9])(?:\s*|\s*[,;]|\s*[,;]\s*([0-9])?)*$

    Test Data
    ---------
    3, 6, 2, 6

    Results
    --------
    Match 1: 3, 6, 2, 6
    Group 1: 3  
    Group 2: 6 

    Required Results (Group)
    ----------------
    Match 1: 3, 6, 2, 6
    Group 1: 1  
    Group 2: 6
    Group 3: 2
    Group 4: 6  


    Required Results (Match)
    ----------------
    Match 1: 3
    Match 2: 6
    Match 3: 2
    Match 4: 6

     Thank you in advance for your help!

     -jd98

View Complete Thread