正则表达式排除关键字集

发布于 2024-07-06 23:20:30 字数 67 浏览 6 评论 0原文

我想要一个在遇到“boon.ini”和“http”等单词时会失败的表达式。 目标是采用此表达式并能够构建任何关键字集。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

羁拥 2024-07-13 23:20:30
^(?:(?!boon\.ini|http).)*$\r?\n?

(取自 RegexBuddy 的库)将匹配任何不包含 boon.ini 和/或 http 的行。 这就是你想要的吗?

^(?:(?!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?

蓝海 2024-07-13 23:20:30

可以使用的替代表达式:

^(?!.*IgnoreMe).*$

^ = 表示行的开头
$ = 表示行尾
(?! Expression) = 表示表达式上的零宽度前瞻负匹配

需要前面的 ^,否则在求值时,负前瞻可能会从其中的某个位置开始/超越“IgnoreMe”文本 - 并在您不想要的地方进行匹配。

例如,如果您使用正则

(?!.*IgnoreMe).*$

表达式:输入“Hello IgnoreMe Please”,这将导致类似:“gnoreMe Please”,因为否定前瞻发现“I”之后没有完整的字符串“IgnoreMe”。

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

一城柳絮吹成雪 2024-07-13 23:20:30

您应该在代码中执行此操作,而不是在表达式中对结果取反。 这样,表达式就变得非常简单。

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

如果 boon.ini 或 http 位于字符串中的任何位置,将返回 true。 由于 \b 或单词边界,它不会匹配像 httpd 或 httpxyzzy 这样的单词。 如果你愿意,你可以删除它们,它也会匹配它们。 要添加更多关键字,只需添加更多管道即可。

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

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
微暖i 2024-07-13 23:20:30

您可以编写一个正则表达式,当遇到您要查找的单词时,该正则表达式会成功,然后反转条件。

例如,在 Perl 中您可以使用:

if (!/boon\.ini|http/) {
    # the string passed!
}

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!
}
二手情话 2024-07-13 23:20:30
^[^£]*$

上面的表达式将仅限制字符串中的井号。 这将允许除字符串之外的所有字符。

^[^£]*$

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

七堇年 2024-07-13 23:20:30

哪种语言/正则表达式库? 我认为您的问题是围绕 ASP.NET 的,在这种情况下您可以看到本文的“负向查找”部分:
http://msdn.microsoft.com/en-us/library/ms972966。 aspx

严格来说,正则表达式的否定,仍然定义了正则语言,但很少有库/语言/工具允许表达它。

否定查找可能为您提供相同的服务,但实际语法取决于您使用的内容。 蒂姆的回答是一个 (?...) 的例子

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

烟火散人牵绊 2024-07-13 23:20:30

我使用这个(基于 Tim Pietzcker 的答案)来排除 Google Analytics 配置文件过滤器的非生产子域 URL:

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

您可以在此处查看上下文:正则表达式排除多个单词

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文