我需要打印perl中存储的所有匹配的字符串。我看到的这个 Print the matched string using perl Perl Regex - Print the matched value在perl中打印匹配的字
各个岗位和我尝试先尝试打印的第一个字。但我得到一个生成错误
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"; }
您能否多给点解释一下,这两种工作方式有什么不同? if($ line =〜/([a-z] ar)i /){print“$ 1 \ n”} if($ line =〜/ [a-z] ar/gi) {print“$ 1 \ n”; } –
一个是全局匹配('g'),另一个不是。 –
不,我的意思是捕捉部分你最近怎么样? –