Reputation: 149
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:
([^,]+,)
beginning of string: any character except comma - one to unlimited times - followed by a comma*
all this zero to unlimited times([^,]*)
end of string: any character except comma - one to unlimited timesRegex101 shows this string matches my pattern:
altdns1.de.de,altdns2.de.de,altdns3.de.de,
Why?
Upvotes: 0
Views: 54
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