Hi, I need assistance please in validating an input string
The string contains the following:
first character must be a Z
second character can be either S, U, or R
Third character can be any number between 1-6
The fourth character can be any character A-Z
The fifth and/or sixth character can be a A-Z

The minimum length of the input string is 4, eg. ZS6P and the maximum length is 6 e.g. ZR5PPT
my regex at this stage is [Z][SUR][1-6][A-Z] but how do i define the last two characters as these last two character might not be necessary. In other words the string could be ZU3P or ZS6BC or ZS1EFN in all three these instances these are valid strings.


The regex so far says the following:

  • The first character must be a “Z”

  • The second character must be an “S”, “U”, or “R”

  • The third character must be a number “1”, “2”, “3”, “4”, “5”, or “6”

  • The fourth character must be a letter “A”, “B”, …, “Z”
    If you use curly braces you can define how many characters you want:

  • [A-Z]{1,3} means that you need at least one and no more than 3 of the preceding set (capital letters)
    Try the following, I think it may solve the problem for you:

  • [Z][SUR][1-6][A-Z]{1,3}