SA__
SA__

Reputation: 1862

Regex except full string not the parts inside the string

I need to write a regex expression to avoid particular string and not the parts of the string

This should not allow only the full word "sup" but it can allow "pus", "su" and all.

How can i make it.

Here is what i tried so far.

[^(?!sup).*$]

Please help

Upvotes: 2

Views: 83

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

Your pattern is close, but the negative lookahead is off. Try using this pattern:

^(?!.*sup).*$

The negative lookahead (?!.*sup) asserts that the pattern never encounters sup at any point in the string.

Note that I also removed the square brackets around the pattern. That made it look like perhaps you were trying to use a character class. But that would not be too helpful here, because you are trying to exclude a sequence of characters, rather than a single character.

Note that if you intend to blacklist the standalone word sup, then you should provide word boundaries in the pattern:

^(?!.*\bsup\b).*$

Upvotes: 2

Jan
Jan

Reputation: 43169

Speaking of a "full word" (not any part), you may be looking for

^(?!.*\bsup\b).*$

Note the word boundaries on either side of the sup and see a demo on regex101.com.

Upvotes: 1

Related Questions