From owner-freebsd-questions@FreeBSD.ORG Sun Nov 30 18:24:32 2008 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 0B9251065670 for ; Sun, 30 Nov 2008 18:24:32 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.freebsd.org (Postfix) with ESMTP id 793FF8FC1A for ; Sun, 30 Nov 2008 18:24:31 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from kobe.laptop (adsl144-35.kln.forthnet.gr [195.74.243.35]) (authenticated bits=128) by igloo.linux.gr (8.14.3/8.14.3/Debian-5) with ESMTP id mAUIOMXX010624 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Sun, 30 Nov 2008 20:24:27 +0200 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.14.3/8.14.3) with ESMTP id mAUIOMxb090757; Sun, 30 Nov 2008 20:24:22 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.14.3/8.14.3/Submit) id mAUIOLRu090756; Sun, 30 Nov 2008 20:24:21 +0200 (EET) (envelope-from keramida@ceid.upatras.gr) From: Giorgos Keramidas To: Drew Tomlinson References: <4932CA0D.8080109@mykitchentable.net> Date: Sun, 30 Nov 2008 20:24:21 +0200 In-Reply-To: <4932CA0D.8080109@mykitchentable.net> (Drew Tomlinson's message of "Sun, 30 Nov 2008 09:14:53 -0800") Message-ID: <873ah9qdgq.fsf@kobe.laptop> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-MailScanner-ID: mAUIOMXX010624 X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-3.8, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.60, BAYES_00 -2.60) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Spam-Status: No Cc: FreeBSD Questions Subject: Re: Regular Expression Help 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: Sun, 30 Nov 2008 18:24:32 -0000 On Sun, 30 Nov 2008 09:14:53 -0800, Drew Tomlinson wrote: > I'm attempting to take an ldiff file and flip first/last name order. > However I can not figure out how to match hyphenated last names. In > vim, my current search/replace string is: > > %s/cn=\(\w\+\-*\) \(\w\+\),/cn=\2 \1,/gc Hi Drew, Depending on the VI implementation you are using this may not work because some versions don't support `\w' as a character class. The regular expession, when translated to Perl, works fine though: $ cat foo cn=Smith Joe, cn=Smith-Brown Joe, $ perl -pe 's/^cn=(\w+-*\w+) (\w+),/cn=$2 $1,/' foo cn=Joe Smith, cn=Joe Smith-Brown, $ So you can just use Perl and the `extended regexp' syntax that includes `\w', `\d' and other special character classes. If you really _have_ to use VI to do this sort of replacement though, you may have to write the fully expanded form of `\w' to make this work: %s/^cn=\([A-Za-z]\+[A-Za-0-9]*[A-Za-z]*\) \([A-Za-z]\+[A-Za-0-9]*[A-Za-z]*\),/cn=\2 \1,/ Using the same \(...\) expression for both the `name' and `surname' part may be useful if you want to switch _back_ to the original format too, since it will swap words for all of the following: cn=Smith Joe, cn=Smith-Brown Joe, cn=Smith Marie, cn=Smith Anne-Marie, cn=Smith-Brown Anne-Marie,