110,538
社区成员
发帖
与我相关
我的任务
分享
Regex singleLineComment = new Regex(@"//(.*)", RegexOptions.Compiled);
Regex multiLineComment = new Regex(@"(?<!/)/\*([^*/]|\*(?!/)|/(?<!\*))*((?=\*/))(\*/)", RegexOptions.Compiled | RegexOptions.Multiline);
Regex RegularString = new Regex("((?<!\\\\)\"([^\"\\\\]|(\\\\.))*\")", RegexOptions.Compiled | RegexOptions.Multiline);
Regex AtString = new Regex("@(\"([^\"]*)\")(\"([^\"]*)\")*", RegexOptions.Multiline | RegexOptions.Compiled);
string text = new StreamReader(@"TextFile1.HTML").ReadToEnd();
Match mat = null;
for (int i = 0; i < text.Length; i++)
{
if (text[i] == '\"')
{
mat = RegularString.Match(text, i);
if (mat.Success)
{
i = mat.Index + mat.Length;
}
}
else if (text[i] == '@')
{
mat = AtString.Match(text, i);
if (mat.Success)
{
i = mat.Index + mat.Length;
}
}
else if (text[i] == '/')
{
mat = singleLineComment.Match(text, i);
if (mat.Success)
{
text = text.Remove(mat.Index, mat.Length);
i--;
}
else
{
mat = multiLineComment.Match(text, i);
if (mat.Success)
{
text = text.Remove(mat.Index, mat.Length);
i--;
}
}
}
}
Console.WriteLine(text);
Regex.Replace(input,@"((?<!:)//[^\n]+|/\*.*?\*/)","");
///<comment></comment>
public string GetExactDescription()
{
return "We can use /*...*/ or //... for comments";
}