Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 5 Oct 2003 11:32:11 -0400 (EDT)
From:      Dan Langille <dan@langille.org>
To:        freebsd-hackers@freebsd.org
Subject:   testing for substrings in perl
Message-ID:  <20031005111656.R18760@xeon.unixathome.org>

next in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20031005111656.R18760>