如何使用Python的正则表达式来匹配GPG签名?

hi3rlvi2  于 2023-02-14  发布在  Python
关注(0)|答案(1)|浏览(137)

我试图看看我是否可以匹配的gpgsig使用下面的正则表达式,但遇到了一个错误也显示如下。
对于如何修复,有什么指导意见吗?

import re

if __name__ == '__main__':
    log = '''
tree e76fa5ccd76492d843b6a4a06038d1c3b5aef6f8
parent 0d533a3a5fd51fd8c2x932832ef9ea91d0756c18
author firstname lastname <userid@company.com> 1676061999 -0800
committer firstname lastname <userid@company.com> 1676061999 -0800
gpgsig -----BEGIN SIGNED MESSAGE-----
 MIAGCSqGSIb3DQEHAqCAMIACAQExDzANBglghkgBZQMEAgEFADCABgkqhkiG9w0B
 BwEAAKCCAuswggLnMIICjKADAgECAhANVjmYTunVjjNs9EhuJ4YXMAoGCCqGSM49
 BAMCME0xKTAnBgNVBAMMIEFmorplIENvcnBvcmF0ZSBTaWduaW5nIEVDQyBDQSAx
 MRMwEQYDVQQKDApBcHBsZSBJbmMuMQswCQYDVQQGEwJVUzAeFw0yMzAyMDkxOTU2
 NTlaFw0yMzAzMDIyMDA2NTlaMDIxEzARBgNVBAoMCkFmorplIEluYy4xGzAZBgNV
 BAMMEmduYWtrYWxhQGFmorplLmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IA
 BGwmvh7HYXCyerdERaLr+OOJ3AQxYNSfUorWkROO2xv/ra8yYGL/aBCYJSQUoYRY
 kY4GE90s8NAUwmQmsthdbFSjggFnMIIBYzAMBgNVHRMBAf8EAjAAMB8GA1UdIwQY
 MBaAFEJi3AGoy1MCpVzt8IjG9uFJdhE9MHMGCCsGAQUFBwEBBGcwZTAvBggrBgEF
 BQcwAoYjaHR0cDovL2NlcnRzLmFmorplLmNvbS9hY3NlY2NhMS5kZXIwMgYIKwYB
 BQUHMAGGJmh0dHA6Ly9vY3NwLmFmorplLmNvbS9vY3NwMDMtYWNzZWNjMTA0MB0G
 A1UdEQQWMBSBEmduYWtrYWxhQGFmorplLmNvbTAUBgNVHSUEDTALBgkqhkiG92Nk
 BBQwMgYDVR0fBCswKTAnoCWgI4YhaHR0cDovL2NybC5hcHBsZS5jb20vYWNzZWNj
 YTEuY3JsMB0GA1UdDgQWBBR1dRRNvQ/7RwRTorG97HmKR4xoJjAOBgNVHQ8BAf8E
 BAMCB4AwJQYDVR0gBB4wHDAMBgoqhkiG92NkBRQBMAwGCiqGSIb3Y2QFFAIwCgYI
 KoZIzj0EAwIDSQAwRgIhAPQ4IiaCG6V5A7u0lwbhJxyXHf9jN2IoqRLj7BlFo4Uv
 AiEAtJAekfgFoiE3h8ZZDgvhwRiwPJseo8GDfM0tb5DP0h8xggE3MIIBMwIBATBh
 ME0xKTAnBgNVBAMMIEFmorplIENvcnBvcmF0ZSBTaWduaW5nIEVDQyBDQSAxMRMw
 EQYDVQQKDApBcHBsZSBJbmMuMQswCQYDVQQGEwJVUwIQDVY5mE7p1Y4zbPRIbieG
 FzANBglghkgBZQMEAgEFAKBpMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJ
 KoZIhvcNAQkFMQ8XDTIzMDIxMDIwNDY1MFowLwYJKoZIhvcNAQkEMSIEIP8j8iYG
 Ggpc74AeVdxLkIArVBLw3+vw6/FVmGtNig+uMAkGByqGSM49AgEERjBEAiB0dBI3
 9c1b/bsStaT3blWb19ehQDt8J/NNov/TzSgEzAIgWvpSs/DZI7wmlHtIJ8HpmIp4
 +oNOu4kJJlhtUy9ZImUAAAAAAAA=
 -----END SIGNED MESSAGE-----

'''

pattern = "gpgsig -----BEGIN SIGNED MESSAGE------{3,}$(?s).*?^-{3,} -----END SIGNED MESSAGE-----"

if re.search(pattern,log):
    print ("Found a match")

下面是错误:

/Users/Documents/pythonscripts/test.py:40: DeprecationWarning: Flags not at the start of the expression 'gpgsig -----BEGIN SI' (truncated)
  if re.search(pattern,log):
2lpgd968

2lpgd9681#

如前所述,DeprecationWarning不是错误,而是警告,在正常情况下不应停止代码执行(因此我将在后面讨论它)。假设期望的结果是匹配所提供的log变量,则该模式存在两个问题:匹配短划线和使用开始/结束(^$)标记。
由于花括号语法,模式测试的破折号数量超过了log变量中的破折号数量,因此找不到匹配项。实际上,模式gpgsig -----BEGIN SIGNED MESSAGE------{3,}仅匹配MESSAGE之后的破折号数量为8或更大的类似文本(其中log中只有5个破折号):

>>> pattern = "gpgsig -----BEGIN SIGNED MESSAGE------{3,}"
>>> re.search(pattern, "gpgsig -----BEGIN SIGNED MESSAGE-------")
>>> re.search(pattern, "gpgsig -----BEGIN SIGNED MESSAGE--------")
<re.Match object; span=(0, 40), match='gpgsig -----BEGIN SIGNED MESSAGE--------'>
>>> re.search(pattern, "gpgsig -----BEGIN SIGNED MESSAGE-----------------")
<re.Match object; span=(0, 49), match='gpgsig -----BEGIN SIGNED MESSAGE----------------->

此外,为便于使用开始/结束标记,应提供MULTILINE标志。
调整代码如下,应与log中的内容一致:

pattern = "gpgsig -{3,}BEGIN SIGNED MESSAGE-{3,}$(?s).*?^ -{3,}END SIGNED MESSAGE-{3,}"

if re.search(pattern, log, flags=re.MULTILINE):
    print("Found a match")

执行结果(请注意,虽然仍发出警告,但现在实际上找到了匹配项):

$ python /tmp/test.py
/tmp/test.py:40: DeprecationWarning: Flags not at the start of the expression 'gpgsig -{3,}BEGIN SI' (truncated)
  if re.search(pattern, log, flags=re.MULTILINE):
Found a match

至于发出的警告,它与内联修饰符组(?s)有关,因为它的python实现/解释可能不会产生预期的结果。我可以想到3种方法来避免这个警告,但每一种都应该仔细检查,看看它是否适合你的用例(如果有的话);
1.将内联修饰符限制为特定模式:

pattern = "gpgsig -{3,}BEGIN SIGNED MESSAGE-{3,}$(?s:.*)?^ -{3,}END SIGNED MESSAGE-{3,}"

1.将内联修改器移动到图案的开头:

pattern = "(?s)gpgsig -{3,}BEGIN SIGNED MESSAGE-{3,}$.*?^ -{3,}END SIGNED MESSAGE-{3,}"

1.使用适当的re标志(re.Sre.DOTALL)代替内联修饰符:

pattern = "gpgsig -{3,}BEGIN SIGNED MESSAGE-{3,}$.*?^ -{3,}END SIGNED MESSAGE-{3,}"
if re.search(pattern, log, flags=re.MULTILINE|re.DOTALL):
    print("Found a match")

相关问题