Ich habe nicht festgestellt, warum der Versuch, eine negierte Zeichenklasse mit Regex zu verwenden. Replace ersetzt keine Zeilenumbrüche durch ein Leerzeichen.C# - Regex.Replace - Ich kann nicht herausfinden, warum Newline nicht ersetzt wird
Hier ist ein Beispielcode:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string testInput = "This is a test. \n This is a newline. \n this is another newline. This is a, comma";
Console.WriteLine(testInput);
//get rid of line breaks and other letters not allowed
string commentFix = Regex.Replace(testInput, @"[^A-Z\sa-z\.0-9\-\:\;\$]", " ");
commentFix = "\"" + commentFix + "\"";
Console.WriteLine("\n");
Console.WriteLine(commentFix);
Console.ReadLine();
}
}
}
Der Ausgang dieses ist:
This is a test.
This is a newline.
this is another newline. This is a, comma
"This is a test.
This is a newline.
this is another newline. This is a comma"
Irgendwelche Ideen? (Danke, das ist meine erste Frage!)
Sie haben nichts in Ihrem Muster, das mit Newline übereinstimmt. Siehe meinen Beitrag hier: http://stackoverflow.com/questions/28743851/regular-expression-to-match-any-vertical-whitespace –
Einfach tun: Regex.Replace (testInput, @ "\ n +", ""); – Quinn