I'm not certain this is the best approach, but you could do something like this:
@"^(?(net$|NET$) |[0-9a-zA-Z-]+$)"
Basically it is checking if the string is "net" or "NET", and if so, it matches it against a space. That will always fail. Otherwise, it does your normal match.
Here's a more detailed explanation.
First of all, it starts with a basic if statement and a start ^
anchor: ^(?(if)then|else)
For the if statement, we check for "net" or |
"NET". Each of these is in front of an end $
anchor so that we know it is not a substring. Here's the result: ^(?(net$|NET$)then|else)
Then if the statement is correct, I matched it against an space " ". Since we know the statement is already "net" or "NET", this part will always fail. That leaves us with this: ^(?(net$|NET$) |else)
Finally, we plug in your previous pattern into the else clause to get: ^(?(net$|NET$) |[0-9a-zA-Z-]+$)
After seeing the above answer, I remembered about negative lookaheads, which simplifys this by not needing a the dumb if statement. Using the lookahead, this answer would look like this:
@"^(?!net$|NET$)[0-9a-zA-Z-]+$"
m/[nN][eE][tT]/
). Then match if that test fails. – PP. Jun 4 at 15:28if (testString.ToLower()!="net"
type logic rather than to roll it all into a single regex? it would be more readable at least but would depend what the context of its use is... – Chris Jun 4 at 15:58