I'm trying to check if the 4th letter in a string is not s or S using the following regular expression.
Regex rx = new Regex(@"A[2-6][025][^sS].*");
In Addition I want corresponding three letter strings to match (e.g. "A30"). Unfortunately the Match check returns false. Does someone know what I'm doing wrong and how I can alter my regex?
rx.Match(test).Success
CodePudding user response:
This should do what you want:
^A[2-6][025](?:[^sS].*|)$
Note the non-capturing group part:
(?:[^sS].*|)
This matches a character that is not s
or S
, followed by any number of characters or an empty string.
CodePudding user response:
First you can check if there is an s or S at fourth character place with the following regex:
^...[sS]
At a second stage you want to check, if there is a combination of A and a number which can be solved with your approach:
A[2-6][025]