From owner-freebsd-questions@FreeBSD.ORG Wed Aug 27 16:45:39 2003 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4F39016A4BF for ; Wed, 27 Aug 2003 16:45:39 -0700 (PDT) Received: from mwinf0103.wanadoo.fr (smtp8.wanadoo.fr [193.252.22.30]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4664743FA3 for ; Wed, 27 Aug 2003 16:45:38 -0700 (PDT) (envelope-from david@landgren.net) Received: from landgren.net (APastourelles-107-1-21-104.w81-51.abo.wanadoo.fr [81.51.116.104]) by mwinf0103.wanadoo.fr (SMTP Server) with ESMTP id E07571BFFFB0; Thu, 28 Aug 2003 01:45:36 +0200 (CEST) Message-ID: <3F4D42FB.7040602@landgren.net> Date: Thu, 28 Aug 2003 01:47:07 +0200 From: David Landgren Organization: Isn't it weird/Looks too obscure to me/Wasting away/And that was their policy User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Roger Williams References: <20030828083316.X43524-100000@edo.naviservers.net> In-Reply-To: <20030828083316.X43524-100000@edo.naviservers.net> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit cc: questions@freebsd.org Subject: Re: Off Topic RegEx Question X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 27 Aug 2003 23:45:39 -0000 Roger Williams wrote: > I know thins is not the place but I know one of you know this one off the > top of your head. > > I have: > > $list = "dog 1 1 1 cat 2 1 snake 111" > and I want to end up with: > dog 1 cat 2 snake 1 > I thought > $list =~ s/ \d \d/ \d/g; > would do the trick, but that gives me: > dog d 1 1 cat d snake d 1 \d in the RHS of the s/// doesn't do much (as you can see...) Try: $list =~ s/(\d)\d*(?: \d+)*/$1/g; Capture a digit, maybe followed by more digits, then followed maybe by groups of space and digits. Replace all that by the captured digit. Note that this will transform: dog 1 4 7 cat 2 1 snake 123 => dog 1 cat 2 snake 1 rather than: dog 1 4 7 cat 2 1 snake 123 => dog 7 cat 2 snake 3 I assume since cat 2 1 => cat 2 that you always want the first digit matched. http://www.perlmonks.org/ is a good place to ask Perl questions. David