manuele manuele - 5 months ago 50
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


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:


Or in one line:

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