简体   繁体   中英

Separating SSL Certificate with Python RegEx

I have a SSL certificate file that contains the Certificate Body, Certificate Chain and Encrypted Private Key, eg

-----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.

I tried many different things, eg 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:

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

Please note you may have to escape the \ by doubling them \\ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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