引用符で囲まれた文字列とすべてを完全に考慮して、Java からコメントを削除する Perl 正規表現があります。理解できないのは、\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);
そんな感じ!