解析此HTML时为什么会出现空白输出?

时间:2014-03-04 17:02:49

标签: html perl

我使用LWP::UserAgentHTML::Parser发送HTTP请求,我想在输出中检索匹配的字符串。这是输出:

OK - Number of results: 1
Name: Catalina:type=Manager,path=/xxxxxx,host=localhost

modelerType: org.apache.tomcat.util.modeler.BaseModelMBean

sessionMaxAliveTime: 29034

duplicates: 0
maxInactiveInterval: 28800

entropy: org.apache.catalina.session.StandardManager@6fefa3e7

activeSessions: 3

sessionCounter: 7

我想检索activeSessions的值。我试过这个:

if ( $response->content()=~/activeSessions:/ ) {
    print ("$1 OK\n");
}

但输出为空白。我也试过这个:

my $parser = HTML::Parser->new( 'text_h' => [ \&text_handler, 'dtext' ] );
$parser->parse( $response->decoded_content );

sub text_handler {
    chomp( my $text = shift );

    if ( $text =~ /activeSessions:/i ) {
        print "Matched: $1\n";
    }
}

但输出仍为空白。为什么呢?

1 个答案:

答案 0 :(得分:3)

而不是

if ( $response->content()=~/activeSessions:/ ) {
   print ("$1 OK\n");
}

if ( $response->content()=~/activeSessions:\s*(\d+)/ ) {
   print ("$1 OK\n");
}