Regex matches a comma separated line, without a comma at the end of the line

I am trying to write a regular expression that will allow me to enter all the characters on the keyboard (even space), but limits the input to a comma at the end of the line. I tried to do this, including all possible characters, but it still does not give me the correct result:

   [RegularExpression("^([a-zA-Z0-9\t\n ./<>?;:\"'!@#$%^&*()[]{}_+=|\\-]+,)*[a-zA-Z0-9\t\n ./<>?;:\"'!@#$%^&*()[]{}_+=|\\-]+$", ErrorMessage = "Comma is not allowed at the end of {0} ")]
+4
source share
3 answers
^.*[^,]$

. * means all char, don't take so long

+2
source
^([a-zA-Z0-9\t\n ./<>?;:\"'!@#$%^&*()[]{}_+=|\\-]+,)*[a-zA-Z0-9\t\n ./<>?;:\"'!@#$%^&*()[]{}_+=|\\-]+(?<!,)$

                                                                                                        ^^

Just add lookbehindto the end.

+1
source

, ( ), .

, , , . , , .

,

(?!,).(?=\r\n|\z)

(- (?=\r\n|$) look-ahead), (?!,) , ( .) . \z .

- regex

.

, .* ( , . ):

.*(?!,).(?=\r\n|\z)

Or (acceleration of work with an atomic group or built-in multi-line option with the ^beginning of line binding, but will not work on the client side)

(?>.*)(?!,).(?=\r\n|\z)
(?m)^.*?(?!,).(?=\r\n|\z) // The fastest of the last three

Watch the demo

0
source

All Articles