Issue
In an ASP.NET form I am trying to find a pattern allowing multiple comma-separated elements but it doesn’t seem to work. I need to allow either 4 letters and 2 digits (JEAN01) or 2 digits and 4 letters (01JEAN) any number of times: JEAN01,JEAN02,03JEAN,JEAN04
My first attempt (see https://regex101.com/r/E4JZVv/1) is:
/^([a-z-A-Z]{4}[0-9]{2}|[a-z-A-Z]{2}[0-9]{4})(,[a-z-A-Z]{4}[0-9]{2}|[a-z-A-Z]{2}[0-9]{4})*$
My second attempt (https://regex101.com/r/HU9cOS/1) is
((^|[,])[a-z-A-Z]{4}[0-9]{2}|[a-z-A-Z]{2}[0-9]{4})+
The first accepts only a couple of elements.
Solution
That should do the trick:
^((\w{4}\d{2}|\d{2}\w{4})(,|$))+
See: https://regex101.com/r/8hoNXl/1
Explanation:
^
: Asserts position at start of the string\w{4}\d{2}
: 4 letters and 2 digits\d{2}\w{4}
: 2 digits and 4 letters(,|$)
: Either,
or the end of the string+
: Repeat between once and an unlimited number of times (greedy)
Answered By – Cubix48
Answer Checked By – Marilyn (BugsFixing Volunteer)