Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using array elements for pattern matching in perl

Tags:

regex

perl

A newbie to Perl and regexes without saying, I am trying to use elements in an array in a perl regex. Here is the snippet

my $temp  = $line =~ s/somestring[^\n]*$_// for @myarray;

If I hard code the string instead of $_ it works fine. Also $_ prints the string fine in isolation. So what am I doing wrong? Even the expanded version of using a for loop doesn't yield a match.

P.S Just to clarify the array has just one element and I know it matches the line.

like image 463
ash Avatar asked Dec 31 '25 19:12

ash


1 Answers

It should work adding parentheses, although I hope that the content of the array hasn't special characters, because you will need to use quotemeta function to escape them.

my $temp;
($temp  = $line) =~ s/somestring[^\n]*$_// for @myarray;
like image 122
Birei Avatar answered Jan 04 '26 14:01

Birei



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!