Quantcast

about submatch in VIM

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

about submatch in VIM

Coiby
Hi, everyone!
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?
Thanks!
ZyX
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: about submatch in VIM

ZyX
Reply to message «about submatch in VIM»,
sent 21:10:08 01 April 2011, Friday
by Coiby:

`\=' must be the at the beginning of the replacement string, so you should use
`\="\/".(submatch(1)+1)' (`-1' is `decrease', not `increase'). By the way, you
could have used `\@<=':
    $s!/\@<=[1-9]\+!\=submatch(0)+1!g

Original message:

> Hi, everyone!
> 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?
> Thanks!
>
> --
> View this message in context:
> http://vim.1045645.n5.nabble.com/about-submatch-in-VIM-tp4273737p4273737.h
> tml Sent from the Vim - General mailing list archive at Nabble.com.

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: about submatch in VIM

Tim Chase
In reply to this post by Coiby
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: about submatch in VIM

Coiby
This post has NOT been accepted by the mailing list yet.
In reply to this post by ZyX
Thanks very much!
Can you recommend any book on talking these feature of VIM. In fact I searched Google, VIM manual and a book by Willey before.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: about submatch in VIM

Coiby
In reply to this post by Tim Chase
Tim, thanks very much for your detailed reply!
I will appreciate it if you recommend some books on such topic.  
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: about submatch in VIM

Tim Chase
On 04/01/2011 07:01 AM, Coiby wrote:
> I will appreciate it if you recommend some books on such topic.

There are several books[1] and websites[2] regarding regular
expressions.  Popular books:

- "Mastering Regular Expressions" (published by O'Reilly)
- "Regular Expression Cookbook" (published by O'Reilly)
- "Regular Expression Pocket Reference" (published by O'Reilly)

Note that dialects & functionalities of regular expression vary
from engine-to-engine, so the underlying ideas remain the same,
but the implementations may differ superficially.

-tim


[1]
http://www.google.com/search?q=regular+expression+books

[2]
http://www.regular-expressions.info
seems pretty decent as a starting-point


--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: about submatch in VIM

David Ohlemacher
I have a couple of those books.  But I keep going back to my Programming Perl book (camel book).  It has a nice and succinct (~10 pages) explanation of regex. It's perlish, but close to many implementations including Boost's.

Boost's docs have a nice reference for the re syntax, but it wont teach you what they mean.
http://www.boost.org/doc/libs/1_46_1/libs/regex/doc/html/boost_regex/syntax.html

-d

On 04/01/2011 07:11 PM, Tim Chase wrote:
On 04/01/2011 07:01 AM, Coiby wrote:
I will appreciate it if you recommend some books on such topic.
There are several books[1] and websites[2] regarding regular 
expressions.  Popular books:

- "Mastering Regular Expressions" (published by O'Reilly)
- "Regular Expression Cookbook" (published by O'Reilly)
- "Regular Expression Pocket Reference" (published by O'Reilly)

Note that dialects & functionalities of regular expression vary 
from engine-to-engine, so the underlying ideas remain the same, 
but the implementations may differ superficially.

-tim


[1]
http://www.google.com/search?q=regular+expression+books

[2]
http://www.regular-expressions.info
seems pretty decent as a starting-point



-- 
David Ohlemacher
Senior Software Engineer
Scientific Solutions Inc. 
99 Perimeter Rd Nashua New Hampshire 03063
603-880-3784

. o .
. . o
o o o

--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: about submatch in VIM

John Little-4
On Apr 5, 3:36 am, David Ohlemacher <[hidden email]> wrote:
> I have a couple of those books.  But I keep going back to my Programming
> Perl book (camel book).  It has a nice and succinct (~10 pages)
> explanation of regex.

Yes,  I used to print out and pin up part of pattern.txt in the Vim
help from \@! to \@>, then I read the camel book and understood.

Regards, John

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