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

Comma-separated token list

Last post 07-07-2008, 7:49 AM by dragonfly-fly999. 4 replies.
Sort Posts: Previous Next
  •  07-03-2008, 2:34 PM 43731

    Comma-separated token list

    I'm trying to come up with a regular expression to validate a comma-separated list of tokens.  The rules are:

    - The token may contain any characters (i.e. alphanumerics, punctuations, ...).
    - There may not be any spaces between the comma and the tokens.
    - The list must not end with a comma.

    Valid lists:

    A
    #$4
    ABC
    ABC,XY"5,t5\6!@,$%45
    Jo*hn,M!^-A,78A$#@2,FGer%^.6

    Invalid lists:

    A,B,
    AG*,HIG,
    ABC,   DEF,       GHI

    Thank you in advance for your help.

  •  07-03-2008, 3:13 PM 43733 in reply to 43731

    Re: Comma-separated token list

    ^(?=(?!.*(?: ,|, |.*,$))).*$

    Test URL:

    http://www.myregextester.com/?r=228


  •  07-03-2008, 3:35 PM 43735 in reply to 43733

    Re: Comma-separated token list

    Thank you for the reply.  I'm a regex newbie and I really want to find out why it works.  Let me go do some reading.
  •  07-03-2008, 4:30 PM 43736 in reply to 43735

    Re: Comma-separated token list

    The regular expression:

    ^(?=(?!.*(?: ,|, |.*,$))).*$

    matches as follows:
     
    NODE                     EXPLANATION
    ----------------------------------------------------------------------
      ^                        the beginning of the string
    ----------------------------------------------------------------------
      (?=                      look ahead to see if there is:
    ----------------------------------------------------------------------
        (?!                      look ahead to see if there is not:
    ----------------------------------------------------------------------
          .*                       any character except \n (0 or more
                                   times (matching the most amount
                                   possible))
    ----------------------------------------------------------------------
          (?:                      group, but do not capture:
    ----------------------------------------------------------------------
             ,                       ' ,'
    ----------------------------------------------------------------------
           |                        OR
    ----------------------------------------------------------------------
            ,                        ', '
    ----------------------------------------------------------------------
           |                        OR
    ----------------------------------------------------------------------
            .*                       any character except \n (0 or more
                                     times (matching the most amount
                                     possible))
    ----------------------------------------------------------------------
            ,                        ','
    ----------------------------------------------------------------------
            $                        before an optional \n, and the end
                                     of a "line"
    ----------------------------------------------------------------------
          )                        end of grouping
    ----------------------------------------------------------------------
        )                        end of look-ahead
    ----------------------------------------------------------------------
      )                        end of look-ahead
    ----------------------------------------------------------------------
      .*                       any character except \n (0 or more times
                               (matching the most amount possible))
    ----------------------------------------------------------------------
      $                        before an optional \n, and the end of
                                the string
    ----------------------------------------------------------------------


  •  07-07-2008, 7:49 AM 43811 in reply to 43736

    Re: Comma-separated token list

    Thank you for the explanation.
View as RSS news feed in XML