I am trying to see if I can match the gpgsig using the regex below, but ran into an error also shown below.
Is there any guidance on how to fix it?
import re
if __name__ == '__main__':
log = '''
tree e76fa5ccd76492d843b6a4a06038d1c3b5aef6f8
parent 0d533a3a5fd51fd8c2x932832ef9ea91d0756c18
author firstname lastname <[email protected]> 1676061999 -0800
committer firstname lastname <[email protected]> 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")
Here is the error:
/Users/Documents/pythonscripts/test.py:40: DeprecationWarning: Flags not at the start of the expression 'gpgsig -----BEGIN SI' (truncated)
if re.search(pattern,log):
As previously commented, the
DeprecationWarningis not an error but rather a warning, and under normal circumstances should not stop code execution (so I'll address it only later). Assuming the desired result is to match thelogvariable as provided, there are two problems with the pattern; matching dashes, and using the start/end (^and$) markers.The amount of dashes tested by the pattern exceeds the amount of dashes in the
logvariable due to the curly-bracket syntax, so no match is found. Effectively, the patterngpgsig -----BEGIN SIGNED MESSAGE------{3,}would only match similar text where the amount of dashes afterMESSAGEis 8 or higher (where inlogthere are only 5 dashes):Additionally, to facilitate the use of start/end markers, the MULTILINE flag should be provided.
Adjusting the code as follows should match the contents in
log:Execution result (note that while the warning is still emitted, now a match is actually found):
As for the emitted warning, it relates to the inline modifier group
(?s), as its python implementation/interpretation might not yield the expected result. I can think of 3 ways to avoid this warning, but each one should be carefully examined to see if it fits your use-case (if at all);re.Sorre.DOTALL) instead of the inline modifier: