简体   繁体   English

正则表达式-字符串中任何地方都不包含某个字符串

[英]Regular expression - does not contain a certain string anywhere within a string

I hope this is not a duplicate, I have been searching reg ex answers for hours so I don't think so! 我希望这不是重复的,我一直在搜索reg ex答案几个小时,所以我不这么认为!

Is there a way of searching with regular expression to say the result must not contain the word 'bot' anywhere in the string? 有没有一种使用正则表达式进行搜索的方式,说结果在字符串的任何地方都不能包含单词“ bot”?

I am using a negative lookahead elsewhere but the problem is I don't know where the word 'bot' may appear. 我在其他地方使用否定前瞻,但问题是我不知道“机器人”一词可能出现在哪里。 It could be part of a longer word, it could appear at the start, at the end, or both! 它可能是一个较长单词的一部分,可能出现在开头,结尾或两者都出现! I want to include results that match the rest of my expression but also exclude 'bot'. 我想包括与表达式其余部分匹配的结果,但也要排除“ bot”。

This is my current search expression: 这是我当前的搜索表达式:

2017-0[2-5] (.*) (UserInfo.aspx UID=111 80 -)=(?!10. *)+. * 

And the results I am searching on generally look like this: 我搜索的结果通常如下所示:

2017-02-16 15:56:00 10.3.1.17 GET /UserInfo.aspx UID=111 80 - 157.85.39.17 Mozilla/5.0+(compatible;+bingbot/2.0;++http://www.bing.com/bingbot.htm) 200 0 0 390

Ideally I would want this result to be excluded as it contains 'bot' even though it still matches the first part of my RegEx. 理想情况下,我希望将此结果排除在外,因为它包含“ bot”,即使它仍与RegEx的第一部分匹配。

Implement this as a two step process if possible. 如果可能,将其实施为两步过程。 If, for example, this is in a script, have two checks: one that tests whether a line contains bot, and then a second step that performs your existing regex, if bot was not there. 例如,如果这是在脚本中,则要进行两项检查:一项检查行是否包含bot,然后进行第二步(如果没有bot,则执行现有的正则表达式)。

Breaking a complex matching requirement into multiple tests is often a good idea. 将复杂的匹配要求分解为多个测试通常是一个好主意。 It results in clearer code, and it also may be more efficient, especially if the earlier checks are simple. 这样可以使代码更清晰,并且效率也可能更高,尤其是在早期检查很简单的情况下。

Breaking this step out separately also allows you to use a simpler positive match instead of a negative match. 分开进行此步骤还可以使您使用更简单的正匹配而不是负匹配。

Example pseudo-code: 伪代码示例:

if (!string.contains('/bot/') and string.contains('/yourpatternhere/'))
{
    do stuff
}

If you are restricted to a single regex, simply use a negative lookahead at the beginning of the string: 如果您只限于一个正则表达式,只需在字符串开头使用负前瞻:

(?!^.*bot)2017-0[2-5] (.*) (UserInfo.aspx UID=111 80 -)=(?!10. *)+. * 

(I'm ignoring any other possible issues with the regex here, which doesn't seem quite right...) (我在这里忽略了正则表达式的任何其他可能的问题,这似乎不太正确...)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写grep正则表达式,以使某个字符串不会出现在当前行上匹配模式之前的任何地方? - How to write a grep regular expression such that a certain string does not appear anywhere before the matching pattern on the current line? 正则表达式匹配字符串中任意位置的一定数量的数字 - Regular expression to match a certain number of digits anywhere in a string 正则表达式-字符串不包含特定字符 - Regular expression - such that a string does not contain specific characters 正则表达式字符串不包含数字 - Regular Expression String Does Not Contain Numbers 正则表达式:匹配不包含特定字符串 - regular expression: matching does not contain specific string 以特定字符串开头的正则表达式,并忽略包含特定字符串的表达式 - Regular expression that starts with a certain string and ignores those that contain a specific string 正则表达式,用于检查字符串是否在某个模式中,该模式可能包含c#中的嵌套括号 - Regular expression to check if a string is within certain pattern that may contain nested parentheses in c# 正则表达式检查字符串是否包含 - Regular expression check if string contain 不允许在字符串中的任何位置使用 '.'(dot)(正则表达式) - Do not allow '.'(dot) anywhere in a string (regular expression) 字符串中任何位置至少 n 位的正则表达式? - Regular expression for at least n digits anywhere in the string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM