0

I have a document that contains lines like

![My title](/path/to/image.jpg)

This is the format for images in Markdown.

I have constructed a regular expression which I believe should be able to find the image tags:

!\[(.*)\]\((.+)\)

But when I try the command /!\[(.*)\]\((.+)\), I get E486: Pattern not found: !\[(.*)\]\((.+)\).


Motivation

I am trying to do this to change how images are presented in my Hugo static site. I want to be able to search and replace everywhere in a file with

%s/!\[(.*)\]\((.+)\)/{{< figure alt="\1" src="\2" >}}/

If it matters, I'm using Vim version 8.0.1283.

  • Debug it step by step, starts from single !. – dedowsdi May 23 at 3:26
  • Please use the correct regular expression items for Vim. So if you want to capture a group, you need to use \(\) instead of () and for capturing at least one of the previous items, you need to use \+ instead of +. – Christian Brabandt May 23 at 5:59
0

Vim doesn't require escaping of parentheses by default.

As Christian says:

[I]f you want to capture a group, you need to use \(\) instead of () and for capturing at least one of the previous items, you need to use \+ instead of +.

Using this modification, my regular expression becomes

!\[\(.*\)\](\(.\+\))

The parentheses which were part of capture groups have been escaped, as has the +, and the parentheses which were literals have been unescaped. This new expression works as intended.

My full search and replace is now

:%s/!\[\(.*\)\](\(.\+\))/{{< figure alt="\1" src="\2" >}}/

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.