testing for substrings in perl

Dan Langille dan at langille.org
Sun Oct 5 08:32:14 PDT 2003


Hi,

I have a perl regex to test if a file resides under a particular
directory.  The test looks like this:

if ($filename =~ $directory) {
   # yes, this filename resides under directory
}

This is working for most cases.  However, it fails is the directory
contains a +.  For example:

$filename = 'ports/www/privoxy+ipv6/files/patch-src::addrlist.c';

$match = "^/?" . 'ports/www/privoxy+ipv6' . "/";
if ($filename =~ $match) {
   print "found\n";
} else{
   print "NOT found\n";
}

Yes, I can escapte the + in the directory name, but then I'd have to test
for all those special regex characters and escape them too.

I think it might just be easier to do a straight comparison of the first N
characters of the two strings where N = length of the directory name.

Any suggestions?

thanks


More information about the freebsd-hackers mailing list