Regular Expression Help
Giorgos Keramidas
keramida at ceid.upatras.gr
Sun Nov 30 10:24:32 PST 2008
On Sun, 30 Nov 2008 09:14:53 -0800, Drew Tomlinson <drew at 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,
More information about the freebsd-questions
mailing list