I've got a random input string to validate and tokenize.
My aim is to check if my string has the following pattern
[a-zA-Z]{2}\d{2} (one or unlimited times) comma separated
So:
aa12,af43,ad46 -> is valid
,aa12,aa44 -> is NOT valid (initial comma)
aa12, -> is NOT valid ( trailing comma)
That's the first part, validation Then, with the same regex I've got to create a group for each occurrence of the pattern (match collection)
So:
aa12,af34,tg53
is valid and must create the following groups
Group 1 -> aa12
Group 2 -> af34
Group 3 -> tg53
Is it possible to have it done with only one regex that validates and creates the groups?
I've written this
^([a-zA-Z]{2}\d{2})(?:(?:[,])([a-zA-Z]{2}\d{2})(?:[,])([a-zA-Z]{2}\d{2}))*(?:[,])([a-zA-Z]{2}\d{2})*|$
but even if it creates the groups more or less correctly, it lacks in the validation process, getting also strings that have a wrong pattern.
Any hints would be very very welcome