Test
[TestCase("-Username ABC -Password XYZ -Address MNO", "-Username ABC -Password *** -Address MNO")]
[TestCase("-Username ABC -Password XYZ", "-Username ABC -Password ***")]
public void Test(string toTest, string expected )
{
Assert.AreEqual(expected, DestroyPassword(toTest));
}
Code
public static string DestroyPassword(string str )
{
var r = new Regex("-Password .*?($| )");//? in .net means not greedy
return r.Replace(str, "-Password ***$1");
}
EDIT:
Word of caution
This doesn't mask passwords with spaces in them. If you know that -Address is always the next token then something like this will be better:
public static string DestroyPassword(string str )
{
var r = new Regex("-Password .*?($| -Address)");//? in .net means not greedy
return r.Replace(str, "-Password ***$1");
}
but event that doesn't deal well with the case when the password contains '-Address'