Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx for an invoice format

Tags:

regex

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}

like image 597
Webleeuw Avatar asked Dec 17 '25 22:12

Webleeuw


1 Answers

As you need the total length to never exceed 10 characters I think you have to handle the three kinds of prefixes separately:

0{3,10}|[JjYy]{2}0{3,8}|[JjYy]{4}0{3,6}
like image 168
Dave Webb Avatar answered Dec 21 '25 14:12

Dave Webb