[英]python, regex, matching strings with repeating characters
我正在嘗試在Apache日志文件中搜索與特定漏洞掃描相關的特定條目。 我需要將單獨文件中的字符串與網絡日志中的URI內容進行匹配。 我嘗試查找的某些字符串包含重復的特殊字符,例如'?'。
例如,我需要能夠匹配僅包含字符串“ ????????”的攻擊 但我不想在字符串“ ??????????????????”上收到警告 因為每種攻擊都與特定的攻擊ID號相關聯。 因此,使用:
if attack_string in log_file_line:
alert_me()
...不管用。 因此,我決定將字符串放入正則表達式中:
if re.findall(r'\%s' % re.escape(attack_string),log_file_line):
alert_me()
...這也不起作用,因為任何包含字符串'????????'的日志文件行 即使超過8個'?' 在日志文件行中。
然后,我嘗試為正則表達式添加邊界:
if re.findall(r'\\B\%s\\B' % re.escape(attack_string),log_file_line):
alert_me()
...在兩種情況下都停止匹配。 我需要能夠動態分配要查找的字符串,但我不想僅在包含該字符串的任何行上進行匹配。 我該怎么做?
怎么樣:
(?:[^?]|^)\?{8}(?:[^?]|$)
說明:
(?-imsx:(?:[^?]|^)\?{8}(?:[^?]|$))
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[^?] any character except: '?'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
\?{8} '?' (8 times)
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[^?] any character except: '?'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.