Shilpesh
Shilpesh

Reputation: 57

Regex formula validation for multiple possible string inputs

Require some help with building Regex formula to validate input string.

Below is my formula that I've come with so far but some parts does not work as per required.

enter image description here

Requirements:-

Acceptable Input String

Samples:

 -'11910281 --> Not Match'    
 -'9U910281 --> Match'    
 -'BC189201 --> Match'    
 -'BC189201.01 --> Match'    
 -'BC189201_01 --> Match'      
 -'BC189201-01 --> Match'

OR String as below

 MTMBX|MTTBX|MTRBX|MTRBX|MTREJ

Not Acceptable Input String With Prefix

 GMSS|GTSS|GRRT|REJS

Upvotes: 0

Views: 46

Answers (1)

The fourth bird
The fourth bird

Reputation: 163277

You could use

^(?!GMSS|GTSS|GRRT|REJS)(?:(?=[^A-Z\s]*[A-Z])[A-Z0-9._-]{8,16}|MTMBX|MTTBX|MTRBX|MTRBX|MTREJ)$

The pattern matches:

  • ^ Start of string
  • (?!GMSS|GTSS|GRRT|REJS) Negative lookahead, assert not any of the alternatives
  • (?: Non capture group
    • (?=[^A-Z\s]*[A-Z]) Positive lookahead, assert at least a single char A-Z
    • [A-Z0-9._-]{8,16} Match any of the listed 8-16 times
    • | Or
    • MTMBX|MTTBX|MTRBX|MTRBX|MTREJ Match any of the allowed alternatives
  • ) Close non capture group
  • $ End of string

Regex demo

Upvotes: 1

Related Questions