Mapi
Mapi

Reputation: 149

RegEx for comma seperated list

I want to validate a list of values seperated by commas, without a comma after the last value:

Shoud be valid:

altdns1.at, altdns2.de.fr, altdns3.de

Should be invalid:

altdns1.at, altdns2.de.fr, altdns3.de,

My RegEx:

^([^,]+,)*([^,]*)$

My thoughts:

Regex101 shows this string matches my pattern:

altdns1.de.de,altdns2.de.de,altdns3.de.de,

Why?

Upvotes: 0

Views: 54

Answers (1)

Toto
Toto

Reputation: 91518

Use this:

^[^,]+(?:,[^,]+)*$

Explanation:

^           : begining of line
  [^,]+     : 1 or more not comma
  (?:       : start non capturing group
    ,       : a comma
    [^,]+   :  1 or more not comma
  )*        : end group, may appear 0 or more times
$           : end of line

Upvotes: 2

Related Questions