From owner-freebsd-hackers@FreeBSD.ORG Sun Oct 5 09:43:26 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 03C2E16A4B3 for ; Sun, 5 Oct 2003 09:43:26 -0700 (PDT) Received: from smtp.infracaninophile.co.uk (ns0.infracaninophile.co.uk [81.2.69.218]) by mx1.FreeBSD.org (Postfix) with ESMTP id 7A13743FBD for ; Sun, 5 Oct 2003 09:43:24 -0700 (PDT) (envelope-from m.seaman@infracaninophile.co.uk) Received: from happy-idiot-talk.infracaninophile.co.uk (localhost [127.0.0.1]) h95GhF9E060895 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 5 Oct 2003 17:43:20 +0100 (BST) (envelope-from matthew@happy-idiot-talk.infracaninophile.co.uk) Received: (from matthew@localhost)id h95GhEKH060894; Sun, 5 Oct 2003 17:43:14 +0100 (BST) (envelope-from matthew) Date: Sun, 5 Oct 2003 17:43:14 +0100 From: Matthew Seaman To: Dan Langille Message-ID: <20031005164314.GA60739@happy-idiot-talk.infracaninophile.co.uk> Mail-Followup-To: Dan Langille , freebsd-hackers@freebsd.org References: <20031005111656.R18760@xeon.unixathome.org> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="GvXjxJ+pjyke8COw" Content-Disposition: inline In-Reply-To: <20031005111656.R18760@xeon.unixathome.org> User-Agent: Mutt/1.5.4i X-Spam-Status: No, hits=-4.9 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=2.60 X-Spam-Checker-Version: SpamAssassin 2.60 (1.212-2003-09-23-exp) on happy-idiot-talk.infracaninophile.co.uk cc: freebsd-hackers@freebsd.org Subject: Re: testing for substrings in perl X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 05 Oct 2003 16:43:26 -0000 --GvXjxJ+pjyke8COw Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sun, Oct 05, 2003 at 11:32:11AM -0400, Dan Langille wrote: > Hi, >=20 > I have a perl regex to test if a file resides under a particular > directory. The test looks like this: >=20 > if ($filename =3D~ $directory) { > # yes, this filename resides under directory > } >=20 > This is working for most cases. However, it fails is the directory > contains a +. For example: >=20 > $filename =3D 'ports/www/privoxy+ipv6/files/patch-src::addrlist.c'; >=20 > $match =3D "^/?" . 'ports/www/privoxy+ipv6' . "/"; > if ($filename =3D~ $match) { > print "found\n"; > } else{ > print "NOT found\n"; > } >=20 > 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 =3D qr{^/?\Q$dirname\E/}; See perldoc perlre for details. Cheers, Matthew =09 --=20 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 --GvXjxJ+pjyke8COw Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.3 (FreeBSD) iD8DBQE/gEoidtESqEQa7a0RAkNZAJ0TKLvKNDTpDHZ2A2gdMovT76+0IgCfSnxR Mf5zNo444doKLwtyLfr0J/8= =eUbW -----END PGP SIGNATURE----- --GvXjxJ+pjyke8COw--