I try to convert a database connection string in the format
postgres://<user>:<password>@<host>:<port>/<database>
to C# connection string
Username=<user>;Password=<dummy>;Host=<host>;Port=<port>;Database=<database>
The following code demonstrates my attempt to dissect the connection string.
public static void Main(string[] args)
{
const string SERVER_DB = "postgres://dummy:dummy123@127.0.0.1:4321/my_db";
Regex templConString = new Regex(@"^<protocol>://<user>:<password>@<host>:<port>/<database>$");
Match match = templConString.Match(Environment.GetEnvironmentVariable("SERVER_DB") ?? SERVER_DB);
Console.WriteLine(match.Groups["protocol"].Value);
Console.WriteLine(match.Groups["user"].Value);
Console.WriteLine(match.Groups["password"].Value);
Console.WriteLine(match.Groups["port"].Value);
Console.WriteLine(match.Groups["database"].Value);
Debugger.Break();
}
The resulting output however is empty. I cannot find a lot of helpful information online to this topic.
EDIT
Regex templConString = new Regex(@"^(?<protocol>\w+)://(?<user>\w+):(?<pass>\w+)@(?<host>[\.\w]+):(?<port>\w+)/(?<database>\w+)/*$");
Gave me what I was looking for