backward reference

24 views
Skip to first unread message

niva

unread,
Oct 30, 2012, 2:22:18 PM10/30/12
to vim...@googlegroups.com
Hi,

I am using this kind of code in order to modify line content :

let patternOfxembh='^.*one="\(\d\+\)".*two="\(\d\+\)"'
if line =~ patternOfxembh
echo line
endif

The recognition is working well and then I can echo my line but is it possible to do an echo of backward reference: value of \(\d\+\).

I would like to avoid to use substitute and using again the same pattern on the line.

Thank you

Ben Fritz

unread,
Oct 30, 2012, 3:23:11 PM10/30/12
to vim...@googlegroups.com
You cannot use the captured groups at a later time after doing a =~ or substitute().

However, you can use the matchlist() function to give you a list of all the matches which you can use later. This function returns an empty list if there are no matches, so to avoid doing regex matching twice you could do:

let lineMatches = matchlist(...)
if !empty(lineMatches)
echo line
...
endif

ni va

unread,
Oct 30, 2012, 4:40:59 PM10/30/12
to vim...@googlegroups.com
It works perfectly :)

2012/10/30 Ben Fritz <fritzo...@gmail.com>

--
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 all
Reply to author
Forward
0 new messages