I have a CSV string that I am trying to validate via regex to ensure it only has N items. I’ve tried the following pattern (which look for 2 items):
/([^,]+){2}/
But it doesn’t seem to work, I am guessing because the inner pattern isn’t greedy enough.
Any ideas? Ideally it should work with both the PHP and Javscript regex engines.
Update:
For technical reasons I really want to do this via regex rather than another solution. The CSV is not quoted and the values will not contain commas, so that isn’t a problem.
/([^,]*[,]{1}[^,]*){1}/
Is where I am at now, which sort of works but is still a bit ugly, and has issues matching one item.
CSV looks like:
apples,bananas,pears,oranges,grapefruit
Got it.
Set the last {N} to the quantity of results or range {1,3} to check.