Replace password in connection string with regular expression in C#

You can use a non-greedy quantifier:

PWD=.*?;

Or exclude ;s:

PWD=[^;]*;

You don't need to use RegEx for this - .NET has the built-in SqlConnectionStringBuilder class which you can use to get values from the connection string and change them.

Example code:

string conString = "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;";
var builder = new SqlConnectionStringBuilder(conString);

builder.Password = "********";

Console.WriteLine(builder.ToString());

Tags:

C#

Regex