正则表达式排除关键字集
我想要一个在遇到“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
(取自 RegexBuddy 的库)将匹配任何不包含 boon.ini 和/或 http 的行。 这就是你想要的吗?
(taken from RegexBuddy's library) will match any line that does not contain boon.ini and/or http. Is that what you wanted?
可以使用的替代表达式:
^
= 表示行的开头$
= 表示行尾(?! Expression)
= 表示表达式上的零宽度前瞻负匹配需要前面的
^
,否则在求值时,负前瞻可能会从其中的某个位置开始/超越“IgnoreMe”文本 - 并在您不想要的地方进行匹配。例如,如果您使用正则
表达式:输入“Hello IgnoreMe Please”,这将导致类似:“gnoreMe Please”,因为否定前瞻发现“I”之后没有完整的字符串“IgnoreMe”。
An alternative expression that could be used:
^
= indicates start of line$
= indicates the end of the line(?! Expression)
= indicates zero width look ahead negative match on the expressionThe
^
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:
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'.
您应该在代码中执行此操作,而不是在表达式中对结果取反。 这样,表达式就变得非常简单。
如果 boon.ini 或 http 位于字符串中的任何位置,将返回
true
。 由于\b
或单词边界,它不会匹配像 httpd 或 httpxyzzy 这样的单词。 如果你愿意,你可以删除它们,它也会匹配它们。 要添加更多关键字,只需添加更多管道即可。Rather than negating the result within the expression, you should do it in your code. That way, the expression becomes pretty simple.
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.您可以编写一个正则表达式,当遇到您要查找的单词时,该正则表达式会成功,然后反转条件。
例如,在 Perl 中您可以使用:
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:
上面的表达式将仅限制字符串中的井号。 这将允许除字符串之外的所有字符。
The above expression will restrict only the pound symbol from the string. This will allow all characters except string.
哪种语言/正则表达式库? 我认为您的问题是围绕 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
(?...)
我使用这个(基于 Tim Pietzcker 的答案)来排除 Google Analytics 配置文件过滤器的非生产子域 URL:
您可以在此处查看上下文:正则表达式排除多个单词
I used this (based on Tim Pietzcker answer) to exclude non-production subdomain URLs for Google Analytics profile filters:
You can see the context here: Regex to Exclude Multiple Words