manuele manuele - 4 days ago 6
Python Question

Database connection string parsing in python

Given a database connection string structure (such like one you can find here) what's the best way to parse a real URI string and get their component like user, password, database name and host?

Thank you very much

Answer

A regular expression (depending on the specific syntax)!?

For example:

m = re.match('mysql://(.*?):(.*?)@(.*?)/(.*)', url)

is supposed to give you user, password, host and database in the groups 1 to 4:

print(m.groups())

Or in one line:

user, password, host, database = re.match('mysql://(.*?):(.*?)@(.*?)/(.*)', url).groups()
Comments