Thread Datenmanipulation: Durchsuchen eines Files und ausgabe in.. (12 answers)
Opened by Gast at 2004-07-18 20:39

format_c
 2004-07-18 22:03
#84589 #84589
User since
2003-08-04
1706 Artikel
HausmeisterIn
[Homepage] [default_avatar]
Da ich bei HTML mir lieber keinen Kopf um RegExes mach hier eine Variante mit HTML::Parser:
Code: (dl )
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
42
43
44
45
46
47
48
49
50
51
52
53
format_c@server:~/perl_scripts> cat test.pl && perl test.pl
#!/usr/bin/perl
use strict;
use HTML::Parser;

my $p = HTML::Parser->new(api_version => 3,
start_h => [\&b_start_handler, "self,tagname,attr"],
#report_tags => [qw(b)],
);
$p->parse_file(*DATA) || die "Konnte nicht parsen: $!";

sub b_start_handler
{
my($self, $tag, $attr) = @_;
return unless $tag eq "b";

$self->handler(text => [], '@{dtext}' );
$self->handler(end => \&b_end_handler, "self,tagname");
}

sub b_end_handler
{
my($self, $tag) = @_;
my $text = join("", @{$self->handler("text")});
$text =~ s/^\s+//;
$text =~ s/\s+$//;
$text =~ s/\s+/ /g;
print "T $text\n" if $text =~ /^name:\s?.*/i;

$self->handler("text", undef);
$self->handler("start", \&b_start_handler);
$self->handler("end", undef);
}
_ _ DATA _ _
<html>
<head>
<title></title>
</head>
<body bgcolor="white">
<i><small><B>Name: Name1</b></small>
<b>
Name: Name2 Nachname2
</B>
<b> Fetter Text</B>
</i>
<b> Name: format_c</b>
</body>
</html>
_ _ END _ _
T Name: Name1
T Name: Name2 Nachname2
T Name: format_c
format_c@server:~/perl_scripts>


Gruß Alex\n\n

<!--EDIT|format_c|1090173958-->

View full thread Datenmanipulation: Durchsuchen eines Files und ausgabe in..