I’m trying to parse a database URL like this
postgres://user:pass@host:port/db?sslmode=require&application_name=test
I cannot find any way to Connect using a Connection string or even parse the URL into a structure or object so that I get the individual fragments to pass it to the Database object.
Am I overlooking something?
should be doable with a regex search.
@Kem_Tekinay will certainly find my approach “not optimized” but here it is :
dim rx as new RegEx
rx.SearchPattern = "(?mi-Us)postgres://(.+):(.+)@(.+):(.+)/db\?sslmode=(.+)&application_name=(.+)"
dim rxOptions as RegExOptions = rx.Options
rxOptions.LineEndType = 4
dim match as RegExMatch = rx.Search( sourceText )
while match isa RegExMatch
//
// Do something
//
dim user as string = rx.subexpressionstring(1)
...
match = rx.Search // Fetch the next match
wend
I’d replace the dot-stars with something more specific like [^:]*
or [^&]*
, but the problem is the order of the parameters. If they are switched, the pattern will fail.
It might be better to use a RegEx for everything up to ?
, then use a regex or Split to pull out each parameter.