Regular Expression to exclude set of Keywords

☆樱花仙子☆ 提交于 2019-11-27 06:29:41
^(?:(?!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?

Tim

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'.

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

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!
}
Manikandan
^[^£]*$

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

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 (?...)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!