【问题标题】:Print text within two RegEx在两个 RegEx 内打印文本
【发布时间】:2012-05-18 04:58:15
【问题描述】:

如何打印从出现开始的文本 reg 表达式 $START_REGEX 直到 $END_REGEX?

#!/usr/bin/perl -w

use strict;
use warnings;

package HTMLStrip;
use base "HTML::Parser";
use LWP::Simple;

my $START_REGEXP = 'To the current program';
my $END_REGEXP = 'Please choose';

sub text {
    my ($self, $text) = @_;
    print $text;
}

my $p = new HTMLStrip;
$p->parse_file("index.html");
$p->eof

【问题讨论】:

    标签: html perl


    【解决方案1】:

    您可以只使用组来获取两个短语之间的值:

    To the current program(.*)Please choose
    

    然后,该值将存储在$1,$2, etc

    Here is the rubular

    对于更多的 perl-cut-and-paste (from this SO question)

    my @values = ($text=~/$START_REGEXP(.*)$END_REGEXP/gm);
    print "The first value is $values[0]\n";
    

    我不是 PERL 开发人员,所以我只是在推断。如果某种语法被关闭,你将不得不自己做进一步的研究。

    【讨论】:

    • 如果我添加 $text =~ /$START_REGEXP(.*)$END_REGEXP/;没有任何变化,它仍然打印所有内容,而不是这些短语之间的文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2020-08-08
    • 2021-03-23
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多