Friday, October 25, 2013

Re: Submatch and regex replace

stosss <stosss@gmail.com> a écrit:
> Starting out with a number at the beginning of the line and want to change
> it from a single or double digit to a triple digit such as 1 to 001 or 10
> to 010 and 100 stays as is
>
> :%s@\(\d\+\)@\=printf("%03d", submatch(1))@
>
> I also want to add some more information around it such as
>
> :%s@\(\d\+\)@<span="b001c001v\1">\1</span>
>
> It would be better to be able to do it in one pass so that I end up going
> from
>
> 1
>
> to
>
> <span="b001c001v001">1</span>
>
> I can do my second :s first and then do my first :s second.
>
> Can I do this all in one pass? I haven't been able to figure out how to do
> what I want using the submatch example above and get the final result that
> I want.

Why not simply combine the two?

:%s@\d\+@\='<span="b001c001v' . printf("%03d", submatch(0)) . '">' . submatch(0) . '</span>'

Not the nicest line in the world, but it should work (note that I've
removed submatches to save a few characters).

Best,
Paul

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment