I have a python code and i’m reading a certificate and matching only the root cert. For ex my certificate is as below:
JavaScript
x
14
14
1
--------begin certificate--------
2
CZImiZPyLGQBGRYFbG9jYWwxGjAYBgoJkiaJk/IasdasdassZAEZFgp2aXJ0dWFsdnB4MSEw
3
HwYDVQQDExh2aXJ0dWFsdnB4LVZJUlRVQUxEQzEtQ0EwHhfdgdgdgfcNMTUwOTE2MTg1MTMx
4
WhcNMTcwOTE2MTkwMTMxWjBaMQswCQYDVQQGEwJVUzEXMBUGCgmSJoaeqasadsmT8ixkARkW
5
B3ZzcGhlcmUxFTATBgoJkiaJk/IsZAEZFgVsb2NhbDEOMAwGA1UEChMFdmNlcnfrrfgfdvQx
6
CzAJBgNVBAMTAkNBMIIBIjANBgkqhkiG9w
7
--------end certificate----------
8
--------begin certificate--------
9
ZGFwOi8vL0NOPXZpcnR1YWx2cHgtcvxcvxvVklSVFVBTERDMS1DQSxDTj1BSUEsQ049UHVi
10
bGljJTIwS2V5JTIwU2VydmldfsfhjZXMsQ049U2VydmfffljZXMsQ049Q29uZmlndXJhdGlv
11
bixEQz12aXJ0dWFsdnB4LERDPWxvY2FsP2NxvxcvxcvBQ2VydGlmaWNhdGU/YmFzZT9vYmpl
12
Y3RDbGFzcz1jZXJ0aWZpY2F0aW9uQXV0dsfsdffraG9yaXR5MD0GCSsGAQQBgjcVBwQwMC4G
13
--------end certificate----------
14
I want to fetch only the root certificate, which starts with CZImiZPy. I read the certificate into the variable data and applying the below regex
JavaScript
1
2
1
re.sub('-----.*?-----', '', data)
2
But it fetched both the encrypted certificates and not just the first one. Is there any better way I can tweak the regular expression?
Advertisement
Answer
You want to search for text, not substitute it with something else.
JavaScript
1
10
10
1
>>> import re
2
>>> s = """--------begin certificate--------
3
<certificate encrypted>
4
--------end certificate----------
5
--------begin certificate--------
6
<certificate encrypted>
7
--------end certificate----------"""
8
>>> re.search(r"-+begin certificate-+s+(.*?)s+-+end certificate-+", s, flags=re.DOTALL).group(1)
9
'<certificate encrypted>'
10
Explanation:
JavaScript
1
6
1
-+begin certificate-+ # Match the starting label
2
s+ # Match whitespace (including linebreaks)
3
(.*?) # Match any number of any character. Capture the result in group 1
4
s+ # Match whitespace (including linebreaks)
5
-+end certificate-+ # Match the ending label
6
re.search()
will always return the first match.