manuele manuele - 1 year ago 155
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 Source

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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download