I need to implement a regex which cover several requirements. These are the following:
I tried many things so far and thought about Lookahead/Lookbehind because of the asterix and the max. length. My latest state which covers the most of the requirements is the following:
^([A-Za-z]{2}[0-9*]{0,6}|\*)$
check the live demo with right/wrong combo
But in this example a code without asterix/wildcard is possible with less than 8 chars -> that's wrong.
Thanks a lot for any help in advance :)
You can use
^(?!.*\*\*$)(?!.{9})(?:[A-Za-z]{2}(?:\d*(?:\*\d*)+|\d{6})|\*)$
See the regex demo.
Details:
^
- start of string(?!.*\*\*$)
- no two **
at the end of string allowed(?!.{9})
- the string must contain less than 9 chars other than line break chars(?:[A-Za-z]{2}(?:\d*(?:\*\d*)+|\d{6})|\*)
- one of the two alternatives:
[A-Za-z]{2}(?:\d*(?:\*\d*)+|\d{6})
- two letters and then either six digits or zero or more digits followed with one or more sequences of an asterisk and zero or more digits|
- or\*
- an asterisk$
- end of string.