Thursday, August 27, 2009

Re: Regex help -> \v + &

* Marcelo Sabino <marcelo.sabino@gmail.com> [2009/8/25]:
> I'm having "troubles" with ER on vi.
> Let me explain. I have this data
> 03;14;Production;trombetas;Xeon;RHEL;-;PPE;-;F1-A0.3;|
> and I want to copy the data of third
> field to the last field, like this
> 03;14;Production;trombetas;Xeon;RHEL;-;PPE;-;F1-A0.3;|trombetas
> How can I do this magic?

* Tomas Kramar:
> Why don't you use a macro? It seems better
> suited for this type of task to me.

* Bastiaan Wakkie <bwakkie@gmail.com> [2009-08-27 12:55]:
> Hmm I guess a macro is indeed much better
> but anyway I worked out a regex for it too:
>
> The line: ^(.+);(.+);(.+);(.+);(.+);(.+);(.+);(.+);(.+);(.+);\|.?$
> the replacement: \1;\2;\3;\5;\6;\7;\8;\9;\10;|\4
>
> If you need to shuffle more I guess the regex comes in handy...
> ...but somehow I could not get this translated back in vim's regex

use some of vim's "real magic" - "\v"!

i think i spotted two typos:
the pipe at the end of the line is already in the given data.
and the replacement is missing the fourth group (\4).

as everything in the search pattern is basicially left
untouched, you can use "&" for that in the replacement.

this brings us down to:
%s/SEARCH/&\4/

however, there can be only nine references to groups.
so there cannot be a reference "\10".
as we only need the fourth group anyway,
the rest can be captured without any groups.

SEARCH: \v^(.+);(.+);(.+);(.+);.+;.+;.+;.+;.+;.+;\|.?$
REPLACE: &|\4

the basic structure:
%s///
%s/SEARCH/REPLACE/

inserting the patterns:
%s/\v^(.+);(.+);(.+);(.+);.+;.+;.+;.+;.+;.+;\|.?$/&|\4/

seems to work. :)

Sven

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

No comments: