Skip site navigation (1)Skip section navigation (2)
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

--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--



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