问题描述
|
我有一个模式匹配器代码,例如:
#!/usr/bin/perl
use strict;
use warnings;
open(HTML,\"<source.html\");
my $html = do {local $/; <HTML>};
$html =~ s/\\n\\ *//g;
while ($html=~m/<OPTION [^>]*>\\D*([^<]+)/g){
print $1;
print \"\\n\";
}
close(HTML)
我想这样做直到文件,但是我想在循环时停止并中断,它会看到与以以下开头的模式匹配的任何字符:
</S
如何使用Perl做到这一点?
解决方法
如果要退出循环,则应使用最后一个命令:
last if ( $pattern ~= /^<\\/S/ );
,( my $to_search = $html ) =~ s{</S.*}{}s;
while ($to_search =~ m{<OPTION [^>]*>\\D*([^<]+)}g) {
print(\"$1\\n\");
}