使用perl在字符串中查找子字符串

使用perl在字符串中查找子字符串,perl,Perl,下面是代码 $string = "any text Affected area : menu Feature to test : diagnostics "; $string1=rindex($string,':'); print "$string1\n"; $string2=substr($string,$string1+1); print "$string2"; 使用上述代码,我可以在“要测试的功能”之后找到字符串,但我想找到受影响区域的字符串,例如菜单。请帮助也许使用和捕获的正则

下面是代码

$string = "any text
Affected area :
menu

Feature to test :

diagnostics
";

$string1=rindex($string,':');
print "$string1\n";

$string2=substr($string,$string1+1);
print "$string2";
使用上述代码,我可以在“要测试的功能”之后找到字符串,但我想找到受影响区域的字符串,例如菜单。请帮助

也许使用和捕获的正则表达式在这里会有所帮助:

use strict;
use warnings;

my $string = "any text
Affected area :
menu

Feature to test :

diagnostics
";

my ($area) = $string =~ /(?<=area :\n)(.+)/;

print $area;

我认为这是某种测试程序。这样做更有意义吗

use strict;
use warnings;

my $feature_to_test;
my $affected_area;
while ( my $line <DATA> ) {
    chomp $line;
    if ( $line =~ /^Affected area\s*:/i ) {
        for (;;) {  #Loop forever (until I tell you to stop i.e.)
            my $line = <DATA>;
            if ( $line !~ /^\s*$/ ) {
               $affected_area = $line;
               last;
            }
        }
    }
    if ( $line =~ /^Affected area\s*:/i ) {
        for (;;) {  #Loop forever (until I tell you to stop i.e.)
            my $line = <DATA>;
            if ( $line !~ /^\s*$/ ) {
               $affected_area = $line;
               last;
            }
        }
    }
    if ( $line =~ /^Feature to test\s*:/i ) {
        for (;;) {  #Loop forever (until I tell you to stop i.e.)
            my $line = <DATA>;
            if ( $line !~ /^\s*$/ ) {
               $feature_to_test = $line;
               last;
            }
        }
    }

}
else {
    print qq("Not a special line: "$line"\n);
}

__DATA__
any text
Affected area :
menu

Feature to test :

diagnostics

将其作为一个循环并在每行中读取,可以使逻辑更清晰、更容易理解。它可能没有那么有效,但是即使在econo-box PC上,用Perl读取数百万行文件也不会花费几秒钟的时间。

我使用相同的命令来获取要测试的字符串后功能:例如像my($test)=$string=~/(?@user1891916-在
测试之后有两行新行:
,请尝试
/(?将Perl的调试器用作
Perl-d your_script.pl
,并在代码中应用断点,这样您就可以找出regexp中的MSS。
use strict;
use warnings;

my $feature_to_test;
my $affected_area;
while ( my $line <DATA> ) {
    chomp $line;
    if ( $line =~ /^Affected area\s*:/i ) {
        for (;;) {  #Loop forever (until I tell you to stop i.e.)
            my $line = <DATA>;
            if ( $line !~ /^\s*$/ ) {
               $affected_area = $line;
               last;
            }
        }
    }
    if ( $line =~ /^Affected area\s*:/i ) {
        for (;;) {  #Loop forever (until I tell you to stop i.e.)
            my $line = <DATA>;
            if ( $line !~ /^\s*$/ ) {
               $affected_area = $line;
               last;
            }
        }
    }
    if ( $line =~ /^Feature to test\s*:/i ) {
        for (;;) {  #Loop forever (until I tell you to stop i.e.)
            my $line = <DATA>;
            if ( $line !~ /^\s*$/ ) {
               $feature_to_test = $line;
               last;
            }
        }
    }

}
else {
    print qq("Not a special line: "$line"\n);
}

__DATA__
any text
Affected area :
menu

Feature to test :

diagnostics
use strict;
use warnings;

my $string = "any text
Affected area :
menu

Feature to test :

diagnostics
";
my @string_list = split /\n/, $string;  #Now, this is split line by line
for my $line ( @string_list ) {
    print "same logic as above...\n";
}