简体   繁体   English

将 SSL 证书与 Python 正则表达式分开

[英]Separating SSL Certificate with Python RegEx

I have a SSL certificate file that contains the Certificate Body, Certificate Chain and Encrypted Private Key, eg我有一个 SSL 证书文件,其中包含证书正文、证书链和加密私钥,例如

-----BEGIN CERTIFICATE-----
...
...
...
-----END CERTIFICATE-----
        -----BEGIN CERTIFICATE-----
...
...
...
-----END CERTIFICATE-----
        -----BEGIN ENCRYPTED PRIVATE KEY-----
...
...
...
-----END ENCRYPTED PRIVATE KEY-----

I'm looking to separate it into it's three different parts using Python's RegEx library, re.我希望使用 Python 的 RegEx 库将它分成三个不同的部分,re。

I tried many different things, eg re.split(r'(-----BEGIN.+?-----(?s).+?-----END.+?-----)', exportCertificateOutput)我尝试了很多不同的东西,例如re.split(r'(-----BEGIN.+?-----(?s).+?-----END.+?-----)', exportCertificateOutput)

Any advice on how to do this?关于如何做到这一点的任何建议? Thanks.谢谢。

Based on the comment by @FailSafe I ended up going with:根据@FailSafe 的评论,我最终选择了:

re.findall("(-----[BEGIN \S\ ]+?-----[\S\s]+?-----[END \S\ ]+?-----)")

Please note you may have to escape the \ by doubling them \\ .请注意,您可能必须通过将它们加倍\\来逃避\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM