Regular expression help – comma delimited string

Sounds like you need an expression like this:

^[0-9a-zA-Z]+(,[0-9a-zA-Z]+)*$

Posix allows for the more self-descriptive version:

^[[:alnum:]]+(,[[:alnum:]]+)*$
^[[:alnum:]]+([[:space:]]*,[[:space:]]*[[:alnum:]]+)*$  // allow whitespace

If you’re willing to admit underscores, too, search for entire words (\w+):

^\w+(,\w+)*$
^\w+(\s*,\s*\w+)*$  // allow whitespaces around the comma

Leave a Comment