0

I have this string :

content htp://ww.mySite.com/image/123kw9ykjm-065n/640/568/25452583.suite />meta property="twitter:suite

and I want to get the link to add it in a list;

Here is my function :

function MyCodeSource()
    execute "normal! gg" 
    let myList = [] 
    silent! exe '%s/\<https.*suite/\=add(myList, submatch(0))/gn'
    execute "normal! u" 
    echo myList
endfunction

The result is :

htp://ww.mySite.com/image/123kw9ykjm-065n/640/568/25452583.suite />meta property="twitter:suite

instead of :

htp://ww.mySite.com/image/123kw9ykjm-065n/640/568/25452583.suite

Can you help me to get this link alone, please?

3
  • 2
    make your regexp non-greedy (:h non-greedy) -- https.\{-}suite.
    – Maxim Kim
    Mar 14 '20 at 10:42
  • 1
    btw, question title is a bit of misleading, there is nothing in description that uses "between two words"
    – Maxim Kim
    Mar 14 '20 at 10:44
  • 1
    Fyi, execute isnt necessary for ex commands like normal and substitute. Welcome to Vi and Vim!
    – D. Ben Knoble
    Mar 14 '20 at 12:20
1

The problem you're having is that it's matching all the way to the last occurrence of the pattern suite on the line.

To solve that, you can either use a non-greedy multi or you can restrict your match to non-whitespace characters only.

A non-greedy multi such as \{-} would solve your issue:

\{-} matches 0 or more of the preceding atom, as few as possible.

This would match only until the first occurrence of suite in the line:

/\<https.\{-}suite

Another approach is to use \S instead of . in your match, to match on non-whitespace characters only.

/\<https\S*suite

That would prevent the regex from going past the whitespace coming after the URL, which would also solve the problem.

0

You could just use \ze after suite, like this:

silent! exe '%s/\<https.*suite\ze/\=add(myList, submatch(0))/gn'

which will set the end of the match at that position.

See:

:help /\ze
4
  • This will not work, it won't change the fact that .* is still greedy. And \ze at the end of the pattern doesn't change much, that's typically the end of the match already.
    – filbranden
    Mar 14 '20 at 13:42
  • 1
    It doesn't work. If I write : /\<https.*suite\>\ze , I have always />meta property="twitter:suite Mar 14 '20 at 13:43
  • Here is the solution : /\<https.\{-}suite\>\ze Thank you very much. Mar 14 '20 at 13:48
  • 1
    silent and execute aren't needed here
    – D. Ben Knoble
    Mar 15 '20 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.