更新:这是C#代码,我认为这不是正确的地方。无论如何,下面是代码。
我使用以下类并获得了良好的结果。
未测试包含在字符串内的注释,例如:
a = "hi /* comment */ there";
a = "hi there // ";
该类能检测一行开头或至少以一个空格开始的注释。所以以下情况是有效的。
a = "hi// there";
a = "hi//there";
这是代码
static public class CommentRemover
{
static readonly RegexOptions ROptions = RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Multiline;
const string SSingleLineComments = @"\s//.*";
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);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine();
DisplayCommandLineHelp();
}
}
}
}
祝你好运。
'
、\'
(不结束字符串)、\\
(\\'
结束字符串,\\\'
不结束)、'..."...'
(这里"
不开始或结束字符串)以及所有'
、"
和\
符号的组合引起的问题。因此,实际上,对于每一行,首先必须解析字符串字面量(或者在解析注释的同时解析字符串字面量),然后删除实际上不是字符串部分的注释。 - YakovL