, 파이썬으로 작업을 수행하는 경우와 마찬가지로,
/* This is a comment
/* that has been re-commented */ possibly /* due to */
various modifications */
정규 표현식 최선의 해결책이 될 수 없습니다. 특히 위의 예 에서처럼 여러 행에 걸쳐있는 경우
마지막으로이 작업을 수행해야했지만 한 번에 한 줄씩 "/ *"(또는 특정 언어의 구분 기호가 무엇이든)의 수를 유지하면서 아무것도 인쇄하지 않았습니다
use strict;
my $infile = $ARGV[0]; # File name
# Slurp up input file in an array
open (FH, "< $infile") or die "Opening: $infile";
my @INPUT_ARRAY = <FH>;
my @ARRAY;
my ($i,$j);
my $line;
# Removes all kind of comments (single-line, multi-line, nested).
# Further parsing will be carried on the stripped lines (in @ARRAY) but
# the error messaging routine will reference the original @INPUT_ARRAY
# so line fragments may contain comments.
my $commentLevel = 0;
for ($i=0; $i < @INPUT_ARRAY; $i++)
{
my @explodedLine = split(//,$INPUT_ARRAY[$i]);
my $resultLine ="";
for ($j=0; $j < @explodedLine; $j++)
{
if ($commentLevel > 0)
{
$resultLine .= " ";
}
if ($explodedLine[$j] eq "/" && $explodedLine[($j+1)] eq "*")
{
$commentLevel++;
next;
}
if ($explodedLine[$j] eq "*" && $explodedLine[($j+1)] eq "/")
{
$commentLevel--;
$j++;
next;
}
if (($commentLevel == 0) || ($explodedLine[$j] eq "\n"))
{
$resultLine .= $explodedLine[$j];
}
}
$ARRAY[$i]=join(" ",$resultLine);
}
close(FH) or die "Closing: $!";
도 참조 http://stackoverflow.com/questions/2578671/where-can-i-find-information-about-perls-special-을 사용할 수 있습니다 변수 –