Dan Langille wrote:
> 
> Hi,

Hello,

> 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';
> $directory = "^/?" . 'ports/www/privoxy+ipv6' . "/";
> if ($filename =~ $directory) {
>    # yes, this filename resides under directory
> }
> 
> Yes, I can escape the + in the directory name, but then I'd have to
> test for all special regex characters and escape them too.  That
> sounds awfully complicated.  ;)
> 
> 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?

You could use the index function:

if ( index( $filename, $directory ) >= 0 ) {
   # yes, this filename resides under directory
}

perldoc -f index


If you want to use a regular expression then you need the quotemeta
escape sequence:

my $filename = 'ports/www/privoxy+ipv6/files/patch-src::addrlist.c';
my $directory = qr(^/?\Qports/www/privoxy+ipv6/\E);
if ( $filename =~ /$directory/ ) {
   # yes, this filename resides under directory
}

perldoc -f quotemeta
perldoc perlre
perldoc perlop



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to