Perl正则表达式 - 动态正则表达式中的特殊字符

Pau*_*aul 0 regex perl

简单的问题(我希望)

我有一个动态字符串,其中包含符号:?,/,等基本上它是我的apache错误文件中的日志行中的URL字符串

我正在解析我的日志文件,我想看看该行中是否存在某个url实例:

要搜索的网址行:"http://www.foo.com?blah"

问号让我失望,就像正则表达式中的任何特殊字符一样.我正在尝试以下方法:

my $test1 = 'my?test';
my $test2 = 'this is a my?test blah test';

if ($test2 =~ /$test1/)    {    print "YES!!! \n";}
else   {     print "NOOOO!!! \n";  }
Run Code Online (Sandbox Code Playgroud)

这打印NOOOO

my $test1 = 'mytest';
my $test2 = 'this is a mytest blah test';

if ($test2 =~ /$test1/)    {    print "YES!!! \n";}
else   {     print "NOOOO!!! \n";  }
Run Code Online (Sandbox Code Playgroud)

这打印是!

我需要快速解决这个问题.

谢谢一堆

ken*_*ytm 7

真的需要正则表达式吗?问题只是一个简单的子字符串搜索...

if (index($test2, $test1) >= 0)    {    print "YES!!! \n";}
else   {     print "NOOOO!!! \n";  }
Run Code Online (Sandbox Code Playgroud)