Regular Expression to exclude set of Keywords
Asked Answered
I

7

53

I want an expression that will fail when it encounters words such as "boon.ini" and "http". The goal would be to take this expression and be able to construct for any set of keywords.

Igloo answered 22/9, 2008 at 19:5 Comment(0)
P
57
^(?:(?!boon\.ini|http).)*$\r?\n?

(taken from RegexBuddy's library) will match any line that does not contain boon.ini and/or http. Is that what you wanted?

Postfix answered 22/9, 2008 at 19:14 Comment(2)
For me, this fails to match on "boon.htt" or "boon.in" - is there a way to ensure the we only fail to match in the event that the entire string in the negative lookaround matches?Cotquean
@AaronParisi: I can't reproduce that: regex101.com/r/XwhGHt/1 - how exactly are you using the regex?Postfix
P
16

An alternative expression that could be used:

^(?!.*IgnoreMe).*$

^ = indicates start of line
$ = indicates the end of the line
(?! Expression) = indicates zero width look ahead negative match on the expression

The ^ at the front is needed, otherwise when evaluated the negative look ahead could start from somewhere within/beyond the 'IgnoreMe' text - and make a match where you don't want it too.

e.g. If you use the regex:

(?!.*IgnoreMe).*$

With the input "Hello IgnoreMe Please", this will will result in something like: "gnoreMe Please" as the negative look ahead finds that there is no complete string 'IgnoreMe' after the 'I'.

Peirsen answered 6/5, 2009 at 21:58 Comment(0)
R
13

Rather than negating the result within the expression, you should do it in your code. That way, the expression becomes pretty simple.

\b(boon\.ini|http)\b

Would return true if boon.ini or http was anywhere in your string. It won't match words like httpd or httpxyzzy because of the \b, or word boundaries. If you want, you could just remove them and it will match those too. To add more keywords, just add more pipes.

\b(boon\.ini|http|foo|bar)\b
Reproach answered 22/9, 2008 at 19:14 Comment(0)
A
4

you might be well served by writing a regex that will succeed when it encounters the words you're looking for, and then invert the condition.

For instance, in perl you'd use:

if (!/boon\.ini|http/) {
    # the string passed!
}
Abbie answered 22/9, 2008 at 19:16 Comment(0)
E
2
^[^£]*$

The above expression will restrict only the pound symbol from the string. This will allow all characters except string.

Emblazonry answered 9/5, 2010 at 16:18 Comment(0)
T
1

Which language/regexp library? I thought you question was around ASP.NET in which case you can see the "negative lookhead" section of this article: http://msdn.microsoft.com/en-us/library/ms972966.aspx

Strictly speaking negation of a regular expression, still defines a regular language but there are very few libraries/languages/tool that allow to express it.

Negative lookahed may serve you the same but the actual syntax depends on what you are using. Tim's answer is an example with (?...)

Tempura answered 22/9, 2008 at 19:29 Comment(0)
C
0

I used this (based on Tim Pietzcker answer) to exclude non-production subdomain URLs for Google Analytics profile filters:

^\w+-*\w*\.(?!(?:alpha(123)*\.|beta(123)*\.|preprod\.)domain\.com).*$

You can see the context here: Regex to Exclude Multiple Words

Crudden answered 28/10, 2013 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.