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

  • A length restriction to max 8 chars should be done (with or without wildcard). In any case the code is never longer than 8 chars.
  • When wildcard is given also lower then 8 digits is allowed. Without wildcard exactly 8 digits are needed.
  • allowed characters are: 0-9A-Za-z* (all digits, all chars, asterix as wildcard)
  • pure wildcard must be possible
  • else the first two digits must contain a 2 chars country code (alpha-numeric) and then only number or wildcards are allowed.
  • after country code wildcard can be used at any place (in the middle, at the end, mutliple asterix/wildcards after each other also allowed)

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 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wiktor Stribiżew