I am facing problems with Perl string matching/searching using both index as well as the =~ operator. I need to search for the string “RT @zaynmalik: Big cover for @cosmopolitanuk ! Boys looking slick http://example.com/FcWA80HI” in a text file.
if($splitlines[1] =~ /RT @zaynmalik: Big cover for @cosmopolitanuk ! Boys looking slick http://example.com/FcWA80HI/){
## Do something ##
}
However, because ‘@‘ is a special character in Perl, I am getting compile errors. Could you suggest me a method to do this? I tried saving the string to a variable like $str, but it did not work (which is understandable).
So, this is what I am doing now,
$max_freq_tweet = 'RT @zaynmalik: Big cover for @cosmopolitanuk ! Boys looking slick http://example.com/FcWA80HI';
if($splitlines[1] =~ /\Q$max_freq_tweet\E/){
print FILE5 "$splitlines2[1] \n";
}
But it still doesn’t seem to be working.
Either escape the
@via a backslash, or use single quotes.If you have a string and want to use it in a regex, you should make sure to protect the meaning via
\Q...\E:This
\QUOT\Edoesn’t prevent array interpolation, but no character in that string will be considered special; without it the.in the string would match any character!