From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17709 invoked by alias); 15 May 2014 16:02:33 -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: 18823 Received: (qmail 23965 invoked from network); 15 May 2014 16:02:27 -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=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_PASS autolearn=ham version=3.3.2 X-AuditID: cbfec7f5-b7fae6d000004d6d-db-5374de04d43d Date: Thu, 15 May 2014 16:32:19 +0100 From: Peter Stephenson To: zsh-users@zsh.org Subject: Re: ${name/pattern/repl} with negated pattern Message-id: <20140515163219.7b60d531@pwslap01u.europe.root.pri> In-reply-to: <20140515122641.GI1629@isis.sigpipe.cz> References: <20140515122641.GI1629@isis.sigpipe.cz> Organization: Samsung Cambridge Solution Centre X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu) MIME-version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupiluLIzCtJLcpLzFFi42I5/e/4VV2WeyXBBu/+SlvsOLmS0YHRY9XB D0wBjFFcNimpOZllqUX6dglcGSvX/2AsmMJT8XzZLvYGxqOcXYycHBICJhIf7z5ihLDFJC7c W8/WxcjFISSwlFHi3/9dbCAJIIdJouGcGojNIqAqse7zWmYQm03AUGLqptlgzSICohLLV2xm B7GFBcwk+rqeAcXZOXgF7CXmyYJEOYFW3Tp8nBliorHE9b0LwabzC+hLXP37iQniBHuJmVfO gE3kFRCU+DH5HguIzSygJbF5WxMrhC0vsXnNW+YJjAKzkJTNQlI2C0nZAkbmVYyiqaXJBcVJ 6blGesWJucWleel6yfm5mxgh4fd1B+PSY1aHGAU4GJV4eDv2FwcLsSaWFVfmHmKU4GBWEuFN vlISLMSbklhZlVqUH19UmpNafIiRiYNTqoFRcsLXn5wflHPkahW4QmUchUzbCn56zpYp3Ld/ Uli9S4Gh1zllptg+ifAj7/I4GU45PHQ8vj6/3c1fx/HwxzlLfHZxXPNOPv1wTt107f+y6ev0 L+Y0TjH09nP69Ssjd/Ehya8v+syOB7SfqJntHljXsSb966LN+hM3iL2UecjufrJ/xpv2eRuU WIozEg21mIuKEwG9Ssj3HQIAAA== On Thu, 15 May 2014 14:26:41 +0200 Roman Neuhauser wrote: > > a=(x/foo y/bar x/baz y/qux) > > echo ${a/#^x\/*} # wtf > /foo /baz > > what's happening here? It took me a while to work it out. /# means "find this pattern at the head of the string and replace it". By the usual rule it's the longest pattern that matches. Note it's not anchored to the end of the string --- I think you need ${a/#%^x\/*} if you stick with "/", however see the discussion below. ^ means "anything, and I do mean anything, that doesn't match the following pattern". You can see that the initial "x" doesn't match the pattern, because the pattern requires the "/" to match. However, x/ or anything longer does match the pattern because of the *. So the longest string at the head that doesn't match the pattern is "x". So it removes that. This has no effect on the y/... bits; the whole string of those fails to match x/*, so they're removed completely. I think you'd be better off with ${a:#^x/*}, which removes anything that matches the pattern after from beginning to end, with no funny business about anchoring. It still has the oddity that you need to negate the pattern after, but as far as I can see this always works logically. I think the way to do it without negating the pattern is to use the (M) flag, saying substitute the matched part, rather than the rest --- the matched part here would usually be everything you're throwing away with this operator. So try ${(M)a:#x/*}. pws