SoFunction
Updated on 2025-04-08

Example of C# method to remove comments based on regularity

This article describes the method of removing comments based on regularity in C#. Share it for your reference, as follows:

string HoverTreeClearMark(string input)
{
  input = (input, @"/\*[\s\S]*?\*/", "", );
  input = (input, @"^\s*//[\s\S]*?$", "", );
  input = (input, @"^\s*$\n", "", );
  input = (input, @"^\s*//[\s\S]*", "", );
  return input;
}

This method can remove /* */ and // comments, as well as remove blank lines

PS: Here are two very convenient regular expression tools for your reference:

JavaScript regular expression online testing tool:
http://tools./regex/javascript

Regular expression online generation tool:
http://tools./regex/create_reg

For more information about C#, please visit the special topic of this site:Summary of the usage of C# regular expressions》、《Summary of C# coding operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.