[英]perl pattern matching in multiline string
我有一个变量,其中包含约100行。 我需要在有网址的地方打印行。
$string = "this is just a test line 1
this is a test line 2
http://somelink1
this is line 4
http://link2
...
...
我只需要打印网址链接。
如何从$ string打印所有与模式匹配的行。 尝试以下代码。
my $resu =~ /(http(s)?://)?([\w-]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?/, $string;
print $resu;
您需要使用/g
修饰符来匹配多行:
use strict;
use warnings;
my $string = <<'END_STR';
this is just a test line 1
this is a test line 2
http://somelink1
this is line 4
http://link2
...
...
END_STR
while ($string =~ m{(.*http://.*)}g) {
print "$1\n";
}
输出:
http://somelink1
http://link2
但是,如果要从文件中提取此数据,最好只逐行读取文件:
while (<$fh>) {
print if m{(.*http://.*)}g;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.