Regex, um Zeilenkommentare von C # @ zu entfern
Ich arbeite an einer Routine zum Entfernen von Blöckenode Zeilenkommentare aus einem C # -Code. Ich habe mir die anderen Beispiele auf der Website angesehen, aber das @ nicht gefundegena Antwort, die ich suche.
Ich kann Blockkommentare (/ * comment * /) in ihrer Gesamtheit mit diesem regulären Ausdruck mit RegexOptions.Singleline abgleichen:
(/\*[\w\W]*\*/)
Und ich kann Zeilenkommentare (// Kommentar) in ihrer Gesamtheit mit diesem regulären Ausdruck mit RegexOptions.Multiline abgleichen:
(//((?!\*/).)*)(?!\*/)[^\r\n]
Hinweis: Ich benutze[^\r\n]
Anstatt von$
weil$
schließt @ e\r
auch im Spiel.
Das tut es jedoch nichtziemlic Arbeite so, wie ich es will.
Hier ist mein Testcode, mit dem ich übereinstimme:
// remove whole line comments
bool broken = false; // remove partial line comments
if (broken == true)
{
return "BROKEN";
}
/* remove block comments
else
{
return "FIXED";
} // do not remove nested comments */ bool working = !broken;
return "NO COMMENT";
Der Blockausdruck stimmt mit @ übere
/* remove block comments
else
{
return "FIXED";
} // do not remove nested comments */
Das ist in Ordnung und gut, aber der Zeilenausdruck stimmt mit @ übere
// remove whole line comments
// remove partial line comments
un
// do not remove nested comments
Auch wenn ich den * / positiven Lookahead nicht zweimal im Zeilenausdruck habe, stimmt er mit @ übere
// do not remove nested comments *
das ichJa wirklic will nicht.
Was ich will, ist ein Ausdruck, der mit Zeichen übereinstimmt, beginnend mit//
, bis zum Ende der Zeile, abernich contain*/
zwischen den//
und Zeilenende.
Auch, um meine Neugier zu befriedigen, kann mir jemand erklären, warum ich den Lookahead zweimal brauche?(//((?!\*/).)*)[^\r\n]
und(//(.)*)(?!\*/)[^\r\n]
wird beide das * enthalten, aber(//((?!\*/).)*)(?!\*/)[^\r\n]
und(//((?!\*/).)*(?!\*/))[^\r\n]
wird nicht.