I would do it as two regex strings. One to capture a line and another to replace the text. But, I agree with OmegaMan's use of a MatchValidator...
string lineRe = @"^[^\r\n]*[\r\n]+";
string re = @"[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000-x10FFFF]";
string test = @"This is a test
this is also a test
here is a special character " + "\x06" + @"
and this is another " + "\x08" + @" in the line
";
string testresult = "";
StringBuilder tmp = new StringBuilder();
bool gotOne = false;
using (TextWriter tr = File.AppendText(@"c:\Users\Les\log.txt"))
{
foreach (Match mx in Regex.Matches(test, lineRe, RegexOptions.Multiline))
{
gotOne = false;
string lineresult = Regex.Replace(mx.Value, re, delegate(Match mx2)
{
if (gotOne == false)
{
gotOne = true;
tr.Write("{0} {1}: {2}", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString(), mx.Value);
}
return "";
});
tmp.Append(lineresult);
}
}
testresult = tmp.ToString();
Console.Write("{0}", testresult);
}
Les Potter, Xalnix Corporation,
Yet Another C# Blog