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