I want to write a simple regular expression to validate a product code as input from the user.
This product code has some rules to be respected as below:
To match those conditions, I have created this regex:
(S)([B|S|T|M])(20)(-)([0|1])
Input to be tested:
Great, it works partially, but how can I create a special condition into group 5 to check those rules?
X15_Light,
Here you go:
(S)((B|S)|(T|M))(20)(-)(?(3)0|(0|1))
(Demo)
The conditional group structure looks like this:
(?(1)Pattern1|Pattern2)
Where (1) is a reference to a group. If group 1 returns a match then match Pattern1 else match Pattern2.