Date: Sun, 5 Oct 2003 17:43:14 +0100 From: Matthew Seaman <m.seaman@infracaninophile.co.uk> To: Dan Langille <dan@langille.org> Cc: freebsd-hackers@freebsd.org Subject: Re: testing for substrings in perl Message-ID: <20031005164314.GA60739@happy-idiot-talk.infracaninophile.co.uk> In-Reply-To: <20031005111656.R18760@xeon.unixathome.org> References: <20031005111656.R18760@xeon.unixathome.org>
next in thread | previous in thread | raw e-mail | index | archive | help
[-- Attachment #1 --]
On Sun, Oct 05, 2003 at 11:32:11AM -0400, Dan Langille wrote:
> 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.
That's why perl has the \Q...\E metasymbols:
Try:
$match = qr{^/?\Q$dirname\E/};
See perldoc perlre for details.
Cheers,
Matthew
--
Dr Matthew J Seaman MA, D.Phil. 26 The Paddocks
Savill Way
PGP: http://www.infracaninophile.co.uk/pgpkey Marlow
Tel: +44 1628 476614 Bucks., SL7 1TH UK
[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (FreeBSD)
iD8DBQE/gEoidtESqEQa7a0RAkNZAJ0TKLvKNDTpDHZ2A2gdMovT76+0IgCfSnxR
Mf5zNo444doKLwtyLfr0J/8=
=eUbW
-----END PGP SIGNATURE-----
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20031005164314.GA60739>
