Vim 中的 Perl 正则表达式?

发布于 2025-01-03 11:21:03 字数 260 浏览 1 评论 0原文

如何在 Vim 中编写这个 perl regex 替换命令? (摘自这个 pandoc epub 教程):

perl -i -0pe \
's/^Insert\s*(.*)\.png\s*\n([^\n]*)$/!\[\2](..\/figures\/\1-tn.png)/mg' \
*/*.markdown

How can I write this perl regex replace command in Vim? (Taken from this pandoc epub tutorial):

perl -i -0pe \
's/^Insert\s*(.*)\.png\s*\n([^\n]*)$/!\[\2](..\/figures\/\1-tn.png)/mg' \
*/*.markdown

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戈亓 2025-01-10 11:21:03

我不能代表 -i -0pe 标志,但正则表达式:

s/^Insert\s*(.*)\.png\s*\n([^\n]*)$/!\[\2](..\/figures\/\1-tn.png)/mg

将是:

s/^Insert\s*\(.*\).png\s*\n\(.*\)$/!\[\2](..\/figures\/\1-tn.png)/g

请注意,您必须转义捕获组,并且我使用 .* 代替第二个捕获组中的 [^\n]*。您不需要多行标志。

I can't speak for the -i -0pe flags, but the regex:

s/^Insert\s*(.*)\.png\s*\n([^\n]*)$/!\[\2](..\/figures\/\1-tn.png)/mg

Would be:

s/^Insert\s*\(.*\).png\s*\n\(.*\)$/!\[\2](..\/figures\/\1-tn.png)/g

Note that you have to escape the capturing groups and that I used .* instead of [^\n]* in the second capturing group. You don't need a multi-line flag.

度的依靠╰つ 2025-01-10 11:21:03

您要寻找的很多内容取决于您的“神奇”设置。有关 vim 字符字面意义的更多信息,请参阅 :help magic

A lot of what you are looking for is dependent on what you "magic" setting is set to. see :help magic for more info on what charctors vim takes literally.

魄砕の薆 2025-01-10 11:21:03

将现有的单行代码变成管道过滤器

perl -pe 's/^Insert\s*(.*)\.png\s*\n([^\n]*)$/!\[\2](..\/figures\/\1-tn.png)/mg'

然后在 Vim 中使用 1G!G:%! 通过该过滤器管道传输当前文件,例如

:%!perl -pe 's/^Insert\s*(.*)\.png\s*\n([^\n]*)$/!\[\2](..\/figures\/\1-tn.png)/mg'

Make your existing one-liner into a pipeline filter

perl -pe 's/^Insert\s*(.*)\.png\s*\n([^\n]*)$/!\[\2](..\/figures\/\1-tn.png)/mg'

Then use 1G!G or :%! in Vim to pipe the current file through that filter, e.g.

:%!perl -pe 's/^Insert\s*(.*)\.png\s*\n([^\n]*)$/!\[\2](..\/figures\/\1-tn.png)/mg'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文