Date: Sun, 30 Nov 2008 20:24:21 +0200 From: Giorgos Keramidas <keramida@ceid.upatras.gr> To: Drew Tomlinson <drew@mykitchentable.net> Cc: FreeBSD Questions <freebsd-questions@freebsd.org> Subject: Re: Regular Expression Help Message-ID: <873ah9qdgq.fsf@kobe.laptop> In-Reply-To: <4932CA0D.8080109@mykitchentable.net> (Drew Tomlinson's message of "Sun, 30 Nov 2008 09:14:53 -0800") References: <4932CA0D.8080109@mykitchentable.net>
next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, 30 Nov 2008 09:14:53 -0800, Drew Tomlinson <drew@mykitchentable.net> 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,
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?873ah9qdgq.fsf>