Update: This is C# code, and I think this is not the right place for it. Anyway, here it is.
I use the following class with good results.
Not tested with comments inside strings, e.g.
a = "hi /* comment */ there";
a = "hi there // ";
The class detects // comments in the beginning of a line or after a space at least. So the following works.
a = "hi// there";
a = "hi//there";
Here is the code
static public class CommentRemover
{
static readonly RegexOptions ROptions = RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Multiline;
const string SSingleLineComments = @"\s//.*"; // comments with // in the beginning of a line or after a space
const string SMultiLineComments = @"/\*[\s\S]*?\*/";
const string SCommentPattern = SSingleLineComments + "|" + SMultiLineComments;
const string SEmptyLinePattern = @"^\s+$[\r\n]*";
static Regex CommentRegex;
static Regex EmptyLineRegex;
static public string RemoveEmptyLines(string Text)
{
if (EmptyLineRegex == null)
EmptyLineRegex = new Regex(SEmptyLinePattern, ROptions);
return EmptyLineRegex.Replace(Text, string.Empty);
}
static public string RemoveComments(string Text)
{
if (CommentRegex == null)
CommentRegex = new Regex(SCommentPattern, ROptions);
return CommentRegex.Replace(Text, string.Empty);
}
static public string RemoveComments(string Text, string Pattern)
{
Regex R = new Regex(Pattern, ROptions);
return R.Replace(Text, string.Empty);
}
static public string Execute(string Text)
{
Text = RemoveComments(Text);
Text = RemoveEmptyLines(Text);
return Text;
}
static public void ExecuteFile(string SourceFilePth, string DestFilePath)
{
string DestFolder = Path.GetDirectoryName(DestFilePath);
Directory.CreateDirectory(DestFolder);
string Text = File.ReadAllText(SourceFilePth);
Text = Execute(Text);
File.WriteAllText(DestFilePath, Text);
}
static public void ExecuteFolder(string FilePattern, string SourcePath, string DestPath, bool Recursive = true)
{
string[] FilePathList = Directory.GetFiles(SourcePath, FilePattern, Recursive? SearchOption.AllDirectories: SearchOption.TopDirectoryOnly);
string FileName;
string DestFilePath;
foreach (string SourceFilePath in FilePathList)
{
FileName = Path.GetFileName(SourceFilePath);
DestFilePath = Path.Combine(DestPath, FileName);
ExecuteFile(SourceFilePath, DestFilePath);
}
}
static public void ExecuteCommandLine(string[] Args)
{
void DisplayCommandLineHelp()
{
string Text = @"
-h, --help Flag. Displays this message. E.g. -h
-s, --source Source folder when the -p is present. Else source filename. E.g. -s C:\app\js or -s C:\app\js\main.js
-d, --dest Dest folder when the -p is present. Else dest filename. E.g. -d C:\app\js\out or -d C:\app\js\out\main.js
-p, --pattern The pattern to use when finding files. E.g. -p *.js
-r, --recursive Flag. Search in sub-folders too. E.g. -r
EXAMPLE
CommentStripper -s .\Source -d .\Dest -p *.js
";
Console.WriteLine(Text.Trim());
}
string Pattern = null;
string Source = null;
string Dest = null;
bool Recursive = false;
bool Help = false;
string Arg;
if (Args.Length > 0)
{
try
{
for (int i = 0; i < Args.Length; i++)
{
Arg = Args[i].ToLower();
switch (Arg)
{
case "-s":
case "--source":
Source = Args[i + 1].Trim();
break;
case "-d":
case "--dest":
Dest = Args[i + 1].Trim();
break;
case "-p":
case "--pattern":
Pattern = Args[i + 1].Trim();
break;
case "-r":
case "--recursive":
Recursive = true;
break;
case "-h":
case "--help":
Help = true;
break;
}
}
if (Help)
{
DisplayCommandLineHelp();
}
else
{
if (!string.IsNullOrWhiteSpace(Pattern))
{
ExecuteFolder(Pattern, Source, Dest, Recursive);
}
else
{
ExecuteFile(Source, Dest);
}
}
// Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine();
DisplayCommandLineHelp();
}
}
}
}
Good luck.