2

I want to extract the url of a markdown link using vimscript with regexes.

Ideally something in these lines:

fun! GetLinkUri(str)
    return match(a:str, '[.*]\((.*)\)', \1)
endfunc

So given a string like: The search engine [Google](https://google.com) blabla It would return https://google.com.

The way the function is described is not the proper use of match. There's any way of doing it with match? There are any other function that does this job?

Jean Carlo Machado
  • 1,480
  • 1
  • 15
  • 25

1 Answers1

8

You are going to need to use matchstr instead of match. But that's not the only problem you have. I would do this:

return matchstr(a:str, '\[.*\](\zs.*\ze)')
  1. [.*] means match one character, that is either a '.' or a '*'. You need to escape the square brackets if you would like to match literal square brackets.

  2. Unfortunately, matchstr can't return an individual subgroup, so I used \zs and \ze to restrict the matching section to what's between the parentheses.

DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61