I am new to regular expression. My input is a string with comma. I need to validate each sub string length is 3 which is split by comma. example: "em1,erg,123,enc" Here sub strings length is 3, so it is valid. If "em1,erg,123,ency", It should be invalid. How to validate in regular expression without any other code?
You can use the following regular expression:
^(?:[^,]{3},)*[^,]{3}$

Edit live on Debuggex
It consists of the following parts:
^ - beginning of the string[^,]{3} - exactly three characters not being a comma, - a comma(?: ... )* - the former wrapped in a (non-capturing) group followed by a *, which lets the contained pattern repeat (zero or more times).[^,]{3} - exactly three characters not being a comma. This is necessary to match the last three characters (which do not have a comma at the end)$ - end of the stringIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With