Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python regular expression how to extract specific string from url?

I am new in python. I want to know how to extract any specific string with help of python regular expression. How to extract only secret key from this string otpauth://totp/%28acd.com%29:test?secret=2LUC3ODYTQEATBXGBGL45VAZFQQO2NPC&issuer=%28far

my expected result will be like this:
2LUC3ODYTQEATBXGBGL45VAZFQQO2NPC so it will grab all sting after = and before &issuer

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

@esqew is exactly right. Notice how easy this is, with the tools you have available.

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.                                                                                            
>>> from urllib.parse import urlparse
>>> x = urlparse("otpauth://totp/%28acd.com%29:test?secret=2LUC3ODYTQEATBXGBGL45VAZFQQO2NPC&issuer=%28far")
>>> x
ParseResult(scheme='otpauth', netloc='totp', path='/%28acd.com%29:test', params='', query='secret=2LUC3ODYTQEATBXGBGL45VAZFQQO2NPC&issuer=%28far', fragment='')
>>> from urllib.parse import parse_qs                                                                                                                                
>>> y = parse_qs(x.query)
>>> y
{'secret': ['2LUC3ODYTQEATBXGBGL45VAZFQQO2NPC'], 'issuer': ['(far']}
>>> y['secret'][0]
'2LUC3ODYTQEATBXGBGL45VAZFQQO2NPC'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading