On Tuesday 01 July 2003 17:36, Werner Schalk wrote:
Der Code klappt auch soweit wunderbar, nur leider kann dieser keine Kommentare erkennen, die Zeilenumbrüche enthalten bzw. sich über mehrere Zeilen erstrecken. Hat jemand dafür eine Idee, die vielleicht sogar ohne zusätzliche Perl-Module auskommt?
Ganzes File in einem Rutsch einlesen und mit Regexp-Option "m" bearbeiten:
perldoc perlre:
m Treat string as multiple lines. That is, change "^"
and "$" from matching the start or end of the string
to matching the start or end of any line anywhere
within the string.
Einlesen auf einen Rutsch:
open( HTML, $infile ) or die "Can't open $infile";
undef $RS; # prepare to read entire file a once
my $html = <HTML>; # read the entire file into $ycp_code
close( HTML );
Und dann über $html in einer while-Schleife immer wieder Kommentare suchen
bzw. suchen und ersetzen, je nachdem, ob Du sie rauswerfen oder nur auswerten willst.
Ich habe mal so etwas ähnliches für YCP-Code geschrieben ("check_ycp", sag, wenn Du es
komplett haben willst - per PM):
while ( $ycp =~
s{ # substitute
( # this will be reference $1
/\* # literal /*
(.*?) # anything (non-greedy - thus "?") - ref $2
\*/ # literal */
) # ref $1 end
( # ref $3 start
.*$ # anything until the very end
) # ref $3 end
}
{ # replacement is an expression (thus flag "e" below)
my $comment = $1;
my $rest = $3;
$comment =~ s/[^\n]*//gms; # delete everything but the newlines
$comment . $rest; # the replacement string
}gmsxe
# "x" allow comments in regexp
# "g" global - replace all occurences
# "m" multi-line input string
# "s" "." matches newlines
)
{
# NOP
# As I understood the doc, this loop around the replacement shouldn't
# be necessary (since the "g" flag is specified), but it is. Strange.
# Can somebody tell me why?
#
# sh@suse.de 2001-05-04
}
Probier's mal.
CU
--
Stefan Hundhammer