Skip to content
Advertisement

python regex to match only first instance

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

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

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

Explanation:

JavaScript

re.search() will always return the first match.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement