Simple question (I hope)
I have a dynamic string which contains symbols: ?, /, etc Basically it's URL string in a log line in my apache error file
I am parsing my log file, I wa开发者_如何转开发nt to see if a certain instance of a url exists in the line:
URL line to search for: "http://www.foo.com?blah"
The question mark throws me off, as with any special characters in regex's. I'm trying the following:
my $test1 = 'my?test';
my $test2 = 'this is a my?test blah test';
if ($test2 =~ /$test1/) { print "YES!!! \n";}
else { print "NOOOO!!! \n"; }
This prints NOOOO
my $test1 = 'mytest';
my $test2 = 'this is a mytest blah test';
if ($test2 =~ /$test1/) { print "YES!!! \n";}
else { print "NOOOO!!! \n"; }
This prints YES!!!
I need this solution quick.
Thanks a bunch
Do you really need regex? The problem is just a simple substring search...
if (index($test2, $test1) >= 0) { print "YES!!! \n";}
else { print "NOOOO!!! \n"; }
maybe try with "\Q" to escape special char
my $test1 = 'my?test';
my $test2 = 'this is a my?test blah test';
if ($test2 =~ /\Q$test1/) { print "YES!!! \n";}
else { print "NOOOO!!! \n"; }
ouput YES!!!
quotemeta can handle special regex characters.
use warnings;
use strict;
my $test1 = quotemeta 'my?test';
my $test2 = 'this is a my?test blah test';
if ($test2 =~ /$test1/) { print "YES!!! \n";}
else { print "NOOOO!!! \n"; }
{
my $test1 = quotemeta 'mytest';
my $test2 = 'this is a mytest blah test';
if ($test2 =~ /$test1/) { print "YES!!! \n";}
else { print "NOOOO!!! \n"; }
}
Prints:
YES!!!
YES!!!
Have you looked in CPAN for an existing module that might help you? From PerlMonks, I found references to Apache::ParseLog and Apache::LogRegEx
精彩评论