Presuming you want to accept both uppercase and lowercase characters:
^[a-zA-Z0-9_]+(,[a-zA-Z0-9_]+)*$
The mentioned site has great information about regular expressions, I recommend reading through it. For now a short explanation:
^
means beginning of the string, so that no other (possibly invalid) characters can precede it. Between [
and ]
is a character class: specifying what characters may follow. [ABC]
for example means an A, a B or a C. You can also specify ranges like [A-E]
, which means an A, B, C, D or E.
In the regular expression above I specify the range a
to z
, A
to Z
(uppercase), 0
to 9
and the single character _
. The +
means that the character, a group or a character from the character class preceding it must appear at least once or more.
The (
and )
group a part of the regular expression. In this case they group the ,
(for the comma-separated list you wanted) and a repetition of the expression so far. The *
means (like the +
) that the group preceding it may appear many times, but with the difference that the *
makes it optional.
So, in short: this expression allows tags consisting of at least one or more characters in the range a-z, A-Z, 0-9 or the character _
, optionally followed by more tags that begin with a ,
, specifying the requirements for a comma-separated list :)