Thursday, March 31, 2011

Re: about submatch in VIM

On 03/31/2011 12:10 PM, Coiby wrote:
> I want to increase the number in this format "/ddd" by one but also keep
> "/".
> But the following command doesn't work:
> $s/\/\([1-9]\+\)/\/\=submatch(1)-1)/g
> Can anyone give a tip?

In addition to what ZyX's comments (particularly about the "\="
needing to be at the beginning of the replacement), I'd use the
"\zs" to mark the beginning of what I want to replace (which also
allows you to tidy up the regexp by not needing "\(...\)" wrapped
around), and use an alternate delimiter to obviate escaping:

:%s@/\zs[1-9]\+@\=submatch(0)-1@g

A few other things of note:

- the lack of "0" is suspicious, so I expect you mean

[1-9][0-9]* " positive numbers
[0-9]\+ " non-negative numbers

- you say you want "increase" the number, but you subtract 1 from
it (easy fix...just change "-" to "+")

- your initial expression has an extra close-paren that doesn't
seem to have a matching open-paren. That may have been a mis-key
when transcribing.


Hope that gives you both a solution and a bit of understanding
regarding what it's doing.

-tim

--
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

No comments:

Post a Comment