我有一个 Perl 正则表达式,它会在充分考虑引用字符串和所有内容的情况下从 Java 中删除注释。唯一不能理解的是使用 \uXXXX 序列的注释或引用。
sub strip_java_comments_and_quotes
{
s!( (?: \" [^\"\\]* (?: \\. [^\"\\]* )* \" )
| (?: \' [^\'\\]* (?: \\. [^\'\\]* )* \' )
| (?: \/\/ [^\n] *)
| (?: \/\* .*? \*\/)
)
!
my $x = $1;
my $first = substr($x, 0, 1);
if ($first eq '/')
{
# Replace comment with equal number of newlines to keep line count consistent
"\n" x ($x =~ tr/\n//);
}
else
{
# Replace quoted string with equal number of newlines to keep line count consistent
$first . ("\n" x ($x =~ tr/\n//)) . $first;
}
!esxg;
}
我将尝试将其转换为 Java:
Pattern re = Pattern.compile(
"( (?: \" [^\"\\\\]* (?: \\\\. [^\"\\\\]* )* \" )" +
"| (?: ' [^'\\\\]* (?: \\\\. [^'\\\\]* )* ' )" +
"| (?: // [^\\n] *)" +
"| (?: /\\* .*? \\*/)" +
")", Pattern.DOTALL | Pattern.COMMENTS);
Matcher m = Pattern.matcher(entireSourceFile);
Stringbuffer replacement = new Stringbuffer();
while (m.find())
{
String match = m.group(1);
String first = match.substring(0, 1);
m.appendReplacement(replacement, ""); // Beware of $n in replacement string!!
if (first.equals("/"))
{
// Replace comment with equal number of newlines to keep line count consistent
replacement.append( match.replaceAll("[^\\n]", ""));
}
else
{
// Replace quoted string with equal number of newlines to keep line count consistent
// Although Java quoted strings aren't legally allowed newlines in them
replacement.append(first).append(match.replaceAll("[^\\n]", "")).append(first);
}
}
m.appendTail(replacement);
类似的东西!