Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So far I have

^[0-9a-zA-Z-]+$

and it works great matching numbers, letters and dashes but now I want to exclude the word net in both upper and lower case.

More specifically,

Match:

net123

123net

word

etc...

Don't Match:

NET

net

how can I add that detail to the Regex?

Thanks!

share|improve this question
 
What do you mean by excluding the word? Skip the whole line if it's present? –  Joachim Isaksson Jun 4 at 15:27
 
In this situation I often test for a regex match for the words I don't want first (m/[nN][eE][tT]/). Then match if that test fails. –  PP. Jun 4 at 15:28
1  
Any reason not to do a more simple if (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
1  
@Chris I'm trying to define the constraints in a route –  NicoTek Jun 4 at 16:06
1  
@NicoTek: Yeah, I thought there might have been a reason you were constrained to needing a single RegEx but thought it worth bringing up (if nothing else for other people's benefit). –  Chris Jun 4 at 16:09
show 3 more comments

3 Answers

up vote 3 down vote accepted

You can use this pattern:

^(?i)([\da-mo-z-]+|n+(?!et)|(?<=\d)net|net\d)+$

explanation:

(?i) case insensitive

[\da-mo-z-] character class without n

| OR

n+(?!et) n 1 or more time not followed by et

(?<=\d)net net preceded by a digit

net\d net and a digit

This pattern exclude strings that contains the word net, but if you want to exclude only strings that are the word net, you can do this:

^(?i)(?!net$)[a-z\d-]+$
share|improve this answer
 
It is a good approach but I want to allow strings such as 123NET –  NicoTek Jun 4 at 15:35
 
This doesn't match net123 –  ydaetskcoR Jun 4 at 15:35
 
@NicoTek: try the edit –  Casimir et Hippolyte Jun 4 at 15:42
 
This is a good answer. I forgot about the negative look ahead, and didn't even know about the inline options. The only thing it doesn't match that wasn't specified are variations of "Net" with upper and lowercase. If those were supposed to not match, then this is good. Otherwise you can change it to ^(?!net$|NET$)[0-9a-zA-Z-]+$ –  LTAcosta Jun 4 at 16:45

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-]+$"
share|improve this answer
 
Which example doesn't work? They all seem to work for me. –  LTAcosta Jun 4 at 16:07
 
I take back my comment and downvote. My error was testing the regular expression in a non .NET environment (gskinner.com/RegExr). Please accept my apologies. –  Greg Jun 4 at 17:09

Simply add a negative lookahead assertion at the beginning like so:

if (Regex.IsMatch(text, @"
    # Match word that is not NET
    ^              # Anchor to start of string.
    (?!net$)       # Assert that entire string is not NET.
    [0-9a-zA-Z-]+  # Match word.
    $              # Anchor to end of string.
    ", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace)) {
    // "Good" word that is not net, NET, Net,...
} else {
    // Not a "good" word.
} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.