如何匹配下一行?Perl正則表達式與LHS組合
sometext_TEXT1.yyy-TEXT1.yyy
anothertext_OTHER.yyy-MAX.yyy
想要從最後刪除- repetative.text
,但只有當它重複。
sometext_TEXT1.yyy
anothertext_OTHER.yyy-MAX.yyy
我試圖
use strictures;
my $text="sometext_TEXT1.xxx-TEXT1.xxx";
$text =~ s/(.*?)(.*)(\s*-\s*$2)/$1$2/;
print "$text\n";
打印
Use of uninitialized value $2 in regexp compilation at a line 3.
與其他詞,尋找下一個split + match
更好的解決方案...
while(<DATA>) {
chomp;
my($first, $second) = split /\s*-\s*/;
s/\s*-\s*$second$// if ($first =~ /$second$/);
print "$_\n";
}
__DATA__
sometext_TEXT1.yyy-TEXT1.yyy
anothertext_OTHER.yyy-MAX.yyy
在替代的匹配部分反向引用必須\ 2而不是$ 2 – user1937198