From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28500 invoked by alias); 25 Nov 2012 18:57:56 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 17435 Received: (qmail 26546 invoked from network); 25 Nov 2012 18:57:53 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 Received-SPF: neutral (ns1.primenet.com.au: 209.85.212.177 is neither permitted nor denied by SPF record at ntlworld.com) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-proxyuser-ip:date:from:to:subject:message-id:in-reply-to :references:x-mailer:mime-version:content-type :content-transfer-encoding:x-gm-message-state; bh=91s1NCFMx1xQcXkWordL6PnJjFce61hcSLgUSmEaX8M=; b=VgoswZL99dcsJ1svSHexGJFJoSKyp+MT1TWvnXW6lY4YaYFpovnjzBEKkNYt3iCHQP 6rbtlHVjd3ytneb4Ocoqqnq8rVE7JyZwiaVIcUG++gbZPwYzk2yQJdI5cauz9Afkpdri uk/kLQirNmohLlIJshmZecMHYO+0CVS70UlPDy597D8Qf1e7NXGL0w2sK0KuyjomnpEg 6hXeAsR55kEyjhwQEwNNuPEhOMsRe2wlChNDyvJzn+OgAvuaZpGPF4iLpBwelmvX41/t 7NOIMK4SC8JBsukaloEIJGj4oHUlUJt00UbQFy7A9mou740NhBdAUAgmNd+eWhOGOQ+R m3zA== X-ProxyUser-IP: 82.8.55.192 Date: Sun, 25 Nov 2012 18:57:41 +0000 From: Peter Stephenson To: "zsh-users@zsh.org" Subject: Re: Regular expression expanding and matching Message-ID: <20121125185741.3e99b7f0@pws-pc.ntlworld.com> In-Reply-To: <50B23D2D.9060603@internecto.net> References: <50B23D2D.9060603@internecto.net> X-Mailer: Claws Mail 3.8.0 (GTK+ 2.24.7; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQlus8ExWg8VDZ9W6G0GFdc08gi8sY4dD7a8a+GhQri2ncZYvuM6YikL4y6Gxxzk8ro0aFzI On Sun, 25 Nov 2012 16:45:49 +0100 Mark van Dijk wrote: > I was trying to match a string with a regular expression as follows: > > --- > #!/usr/local/bin/zsh > zmodload zsh/pcre > somestring="121125" > todaysday="25" > yesterday="24" > > set -xv > if [[ $somestring -pcre-match \d{4}${todaysday} ]]; then > echo "somestring matches today" > elif [[ $somestring -pcre-match \d{4}${yesterday} ]]; then > echo "somestring matches yesterday" > fi > set +xv > > Apparently there is no expansion of ${todaysday} and ${yesterday}. This > is not really surprising because in regular expressions many characters > have a different meaning. I think the problem is different from what you think it is. Try (note doubled backslashes): if [[ $somestring -pcre-match \\d{4}${todaysday} ]]; then echo "somestring matches today" elif [[ $somestring -pcre-match \\d{4}${yesterday} ]]; then echo "somestring matches yesterday" fi It's not that the $... isn't expanded, it's that the \d is also handled like a normal command line argument. Vin's change works because in double quotes \d remains \d because it doesn't have a special meaning. To put it more broadly, arguments in [[ ... ]] get expanded in all the ways that make sense for generating a single word. So file name generation (globbing) doesn't happen, and array substitution produces a single word (as if double quotes surrounded the array), but otherwise it works like a normal command line argument. So the normal quoting rule for backslashes apply. -- Peter Stephenson Web page now at http://homepage.ntlworld.com/p.w.stephenson/