Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression - check comma separated sub strings length

Tags:

regex

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?

like image 484
Ramesh Avatar asked Jun 08 '26 16:06

Ramesh


1 Answers

You can use the following regular expression:

^(?:[^,]{3},)*[^,]{3}$

Regular expression image

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 string
like image 88
tessi Avatar answered Jun 10 '26 06:06

tessi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!