1 2 3
my $alles = join('', @lines); $alles =~ s/\/\*.+?\*\//gs; my @new_lines = split(/\n/, $alles);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
my @array = qw{ eins zwei/*test drei vier test*/fuenf sechs }; my @new; my $found = 0; for my $string (@array) { if ($found) { if ($string =~ s{ .* \*/ }{}x) { $found = 0; push @new, $string; } } else { if ($string =~ s{ /\* .* }{}x) { $found = 1; } push @new, $string; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for $string (@header2){
if ($found) {
if ($string =~ s{ .* \*/ }{}x) {
$found = 0;
push @neu, $string;
}
}
else {
if ($string =~ s{ /\* .* }{}x) {
$found = 1;
}
push @neu, $string;
}
}
1
2
3
4
5
6
/* a handle to an object is opaque as far as the client is concerned */
struct ps_handle;
typedef struct ps_handle ps_handle_t;
/* the object id is opaque as far as the user is concerned */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#! /usr/bin/perl use strict; use warnings; my @data = ( 'eins', 'zwei /*', 'drei', 'vier */', 'fuenf /* bla */', 'sechs /* foo */ sieben', ); my $found = 0; for my $element ( @data ) { # full comment if ( $element =~ s{ /\* .*? \*/ }{}x ) { next; } # beginning of comment if ( $element =~ s{ /\* .*? }{}x ) { $found = 1; } # end of comment elsif ( $element =~ s{ .*? \*/ }{}x ) { $found = 0; } # lines between beginning and end of comment elsif ( 1 == $found ) { $element = undef; next; } } # undefinierte und leere Elemente werden entfernt @data = grep { defined $_ && 0 < length $_ } @data; use Data::Dumper; print Dumper \@data;
2012-08-13T13:01:16 LinuxerEinfacher Schnellschuß ohne Optimierung oder langes Grübeln, der auch Kommentare auf einer Zeile berücksichtigt.
QuoteAber immer alles Mist, wenn versucht wird, mit REs echten Quelltext zu parsen.
$element =~ s{ /\* .*? \*/ }{}x
1 2 3 4 5 6
for my $string (@array) { my $x = $string =~ s{ /\* .* }{}x .. $string =~ s{ .* \*/ }{}x; if (not $x or $x < 2 or $x =~ m/E0$/) { push @new, $string; } }