Search code examples
regexdigitscsv

Regex check digit lengths in separated string


How can I validate a string that separated by commas and I have to check the records lengths one by one.

Example: 1234567890,123456,1234567890,12345

I would like to do a regex that returns all the not correct number lengths (not equals 10) Where I want to use this pattern don't have any loop or split function so that's why I want to make this with Regex.


Solution

  • The following Regex matches all the numbers not equal by 10, either from the start, after a comma, or at the end of the string.

    (?<=^|,)(\d{1,9}|\d{11,})(?=,|$)
    

    RegExr link

    Note: Positive lookbehinds aren't supported by all browsers. Lookbehind Browser compatibility