From owner-freebsd-hackers@FreeBSD.ORG Sun Oct 5 08:32:14 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 746AB16A4B3 for ; Sun, 5 Oct 2003 08:32:14 -0700 (PDT) Received: from xeon.unixathome.org (bast.unixathome.org [66.11.174.150]) by mx1.FreeBSD.org (Postfix) with ESMTP id 07FDB43FE3 for ; Sun, 5 Oct 2003 08:32:13 -0700 (PDT) (envelope-from dan@langille.org) Received: by xeon.unixathome.org (Postfix, from userid 1000) id 4FCC43E4F; Sun, 5 Oct 2003 11:32:11 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by xeon.unixathome.org (Postfix) with ESMTP id 4636C3E4E for ; Sun, 5 Oct 2003 11:32:11 -0400 (EDT) Date: Sun, 5 Oct 2003 11:32:11 -0400 (EDT) From: Dan Langille X-X-Sender: dan@xeon.unixathome.org To: freebsd-hackers@freebsd.org Message-ID: <20031005111656.R18760@xeon.unixathome.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Subject: 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 15:32:14 -0000 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