regexexpressionwildcardmaxlengthrequirements

Regex to match code with fixed country code and variable wildcard usage


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 :)


Solution

  • You can use

    ^(?!.*\*\*$)(?!.{9})(?:[A-Za-z]{2}(?:\d*(?:\*\d*)+|\d{6})|\*)$
    

    See the regex demo.

    Details: