I’m quite new to regular expressions and I’m trying to create a regex for the validation of an invoice format.
The pattern should be:
JjYy (all 4 characters are legit), used 0, 2 or 4 times
e.g. no Y’s at all is valid, YY is valid, YYYY is valid, but YYY should fail.
Followed by a series of 0’s repeating 3 to 10 times.
The whole should never exceed 10 characters.
examples:
JyjY000000 is valid (albeit quite strange)
YY000 is valid
000000 is valid
jjj000 is invalid
jjjj0 is invalid
I learned some basics from here, but my regex fails when it shouldn’t. Can someone assist in improving it?
My regex so far is: [JjYy]{0}|[JjYy]{2}|[JjYy]{4}[0]{3,10}.
The following failed also: [JjYy]{0|2|4}[0]{3,10}
As you need the total length to never exceed 10 characters I think you have to handle the three kinds of prefixes separately: