【发布时间】:2017-07-14 18:34:15
【问题描述】:
我需要从 perl 中存储的行中打印所有匹配的字符串。我看过这方面的各种帖子 Print the matched string using perl Perl Regex - Print the matched value
我尝试先尝试打印第一个单词。但我得到一个构建错误
Use of uninitialized value $1 in concatenation (.) or string at rg.pl line 10.
我尝试过拆分和数组,它可以工作,但是在打印 $1 时,它会抛出错误。
我的代码在这里
#!/usr/bin/perl/
use warnings;
use strict;
#my $line = "At a far distance near the bar, was a parked car. Star were shining in the night. The boy in the car had scar and he was at war with his enemy. \n";
my $line = "At a far distance near the bar, was a parked car. \n";
if($line =~ /[a-z]ar/gi)
{ print "$1 \n"; }
$_ = $line;
我希望这段代码的输出是
far
并随后打印所有包含 ar 的单词,
far
near
bar
parked
car
我什至尝试更改我的代码,如下所示,但没有奏效,同样的错误
if($line =~ /[a-z]ar/gi) {
my $match = $1;
print "$match \n"; }
【问题讨论】: