From owner-freebsd-questions@FreeBSD.ORG Wed Sep 9 17:46:48 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0685E106566B for ; Wed, 9 Sep 2009 17:46:48 +0000 (UTC) (envelope-from freebsd@optimis.net) Received: from mail.optimis.net (mail.optimis.net [69.104.191.124]) by mx1.freebsd.org (Postfix) with ESMTP id CE43F8FC21 for ; Wed, 9 Sep 2009 17:46:47 +0000 (UTC) Received: from marvin.optimis.net (marvin.optimis.net [192.168.1.3]) by mail.optimis.net (8.14.3/8.14.2) with ESMTP id n89Hklkc016252 for ; Wed, 9 Sep 2009 10:46:47 -0700 (PDT) (envelope-from freebsd@optimis.net) Received: from marvin.optimis.net (localhost [127.0.0.1]) by marvin.optimis.net (8.14.3/8.14.3) with ESMTP id n89Hkl3D065788 for ; Wed, 9 Sep 2009 10:46:47 -0700 (PDT) (envelope-from freebsd@optimis.net) Received: (from george@localhost) by marvin.optimis.net (8.14.3/8.14.3/Submit) id n89Hklc2065787 for freebsd-questions@freebsd.org; Wed, 9 Sep 2009 10:46:47 -0700 (PDT) (envelope-from freebsd@optimis.net) Date: Wed, 9 Sep 2009 10:46:47 -0700 From: George Davidovich To: freebsd-questions@freebsd.org Message-ID: <20090909174647.GA65123@marvin.optimis.net> References: <4AA7D49D.8090002@mykitchentable.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4AA7D49D.8090002@mykitchentable.net> User-Agent: Mutt/1.5.19 (2009-01-05) Subject: Re: Regex Help - Greedy vs. Non-Greedy X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Sep 2009 17:46:48 -0000 On Wed, Sep 09, 2009 at 09:15:25AM -0700, Drew Tomlinson wrote: > I'm trying to do a search and replace in vim. I have lines like this: > http://site1/dir/; > http://site2/dir/;LastName, FirstName;Phone; > http://site3/dir/;LastName, FirstName; > http://site4/dir/; > > I'm want to match "http:*" and stop matching at the first ";". My basic > regex is: > > /http:.\+;/ > > But it's matching *all* the semi-colons. Thus I've Googled and tried > various incatations to try and make my regex "non-greedy" but I can't > seem to come up with the correct combination. LOL. Do yourself a favour and stop "Googling". Vim has a built-in help system. To access help for regular expressions: :help regexp :he regexp :he r[TAB] ... and search for non-greedy (or just scroll down). > How can I write a regex that stops matching at the first semi-colon? You've already received a few answers, but I'll add one that may be even better -- rely on external programs instead. Vim can be a bit clunky at times. The simplest and most typical usage would be :[range] !command If using Perl $ perldoc -h $ perldoc -q regex -- George