python,正则表达式,匹配具有重复字符的字符串

标签 python regex

我正在尝试在 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
----------------------------------------------------------------------

关于python,正则表达式,匹配具有重复字符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12719205/

相关文章:

python - 使用 matplotlib 创建全天空投影的好奇(坏?)行为

java - 需要帮助修改正则表达式

JavaScript RegExp [^...] 表达式未给出预期结果

Python如何使用分割定界符删除csv上的空格

python - 正则表达式和八进制字符

ruby - 使用 ruby​​ 从文本中删除 url

python - 将 API 生成的 PDF 发送到外部服务器?

python - 值错误 : tnc: invalid gradient vector from minimized function

python - 在 CherryPy 中停止请求处理并从工具返回 200 响应

python - 如何通过boto3获取AWS的配额?