As part of cleaning up config files in a build script, we have something like this:
Regex.IsMatch(LongStringOfFilecontents, @"Password=""[0-9a-zA-Z]*""")
and
Regex.IsMatch(LongStringOfFilecontents, @"Password2=""[0-9a-zA-Z]*""")
When a match is found, the passwords are replaced with a dummy value before the app is released.
The problem is that it now finds "Password" but not "Password2" or "Password1".
This C# .NET 3.5 code has been in use for several years, has been run hundreds of times, and has not been changed. As recently as a few days ago it was run successfully. As of this morning it chokes on "Password2". The config file really does contain both Password="some arbitrary value" and Password2="some arbitrary value".
I suspected that "d2" might be taken as a pattern, but it is not inside a {}, and as mentioned, it has behaved correctly for several years.
I have tested against a possible timeout and that does not seem to be the issue. I have tried the CaseInsensitive Option, which should not matter anyway ([a-zA-Z], right?) and that also has no effect.
It fails on two different (Win 7 Professional, 64 bit, SP1) machines, but works as expected on an XP machine (SP 3).
Unless this was the result of this morning's Windows 7 Automatic Update I'm baffled.
Here's the complete context:
ReplaceInFile(filename, @"Password1=""[0-9a-zA-Z]*""", @"Password1=""REPLACE_ME""");
private static bool ReplaceInFile(string filename, string regexp, string replacement)
{
try
{
if (File.Exists(filename))
{
string oldContents = null;
using (StreamReader reader = new StreamReader(filename, true))
{
oldContents = reader.ReadToEnd();
}
if (Regex.IsMatch(oldContents, regexp))
{
string newContents = Regex.Replace(oldContents, regexp, replacement);
if (oldContents != newContents)
{
File.WriteAllText(filename, newContents);
return true;
}
}
else
{
BuildFailed("DID NOT FIND " + regexp + " in " + filename + " Case-SeNsiTive?");
}
}
return false;
}
catch (Exception ex)
{
BuildFailed(ex.Message);
return false;
}
}
And here's a small portion of the large file that it being examined:
<Kirk Enabled="1" Type="8000" Password1="test_pwd" Password2="dev_pwd" UserName1="admin" UserName2="GW-DECT/admin" DutyCycle="1" TcpPort="10000" ServerIP="localhost" />
I think your problem is more likely to be the contents of the password does not match [0-9a-zA-Z]
rather than anything with the Password=
or Password2=
part. Most likely there is a non alpha-numeric character in that password.
Give this regex a try (using on RegexPal.com seemed to work). I did escape the equal sign.
@"Password[1-3]\\=""[0-9a-zA-Z]*"""
Here was my test text:
Password1="Blah"
Password2="blah2"
Password3="045and2"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.