在 Neovim 上使用负向前瞻搜索伪 XML 文件中未转义的 & 符号

问题描述 投票:0回答:1

我不知道如何转换/相当于 Neovim 上的负向先行搜索。

&(?!(?:apos|quot|[gl]t|amp);|#)

当我尝试银色搜索时,它正在工作。我想搜索,但只能使用

/

搜索单个文件
regex vim regex-lookarounds neovim negative-lookbehind
1个回答
0
投票

你的问题很有趣,我实际上经常遇到麻烦 Vim 或其他工具中的正则表达式语法 使用 PCRE 或通用语法!

谷歌搜索一下,我发现了 这篇关于 Vim 中的环视的文章。

如您所见,这只是语法问题!

负向前瞻应写为

(?!amp;)
,例如
\(amp;\)\@!

这会导致这样的事情:

&\(\(\w\{2,6\}\|#\d\{1,6\}\|#[xX][0-9a-fA-F]\{1,6\}\);\)\@!

我将

&
&apos
与 PCRE 中的
&\w{2,6};
相匹配,变成
&\w\{2,6\};
Vim 的语法中。

在此 XML 上进行了测试:

<note>
  <author>Jani</author>
  <heading>Reminder</heading>
  <summary>Glasses &amp; sunscreen</summary>
  <body>
    Don't forget to pack your glasses & sunscreen to go to
    the beach tomorrow!
    If you forget your glasses &#x1F60E; -&gt; you'll damage your retina.
    If you don't put some sunscreen on -&gt; you'll probably get sun burnt &#127774;.
    Now, the problem is to match the ampersand in "H&M" &#128556;!
    &lt; = &#60;
    &gt; = &#62;
    &amp; = &
    &nbsp; = non-breaking space
    &iexcl; = ¡
    &cent; = ¢
    &pound; = £
    &curren; = ¤
    &yen; = ¥
    &brvbar; = ¦
    &sect; = §
    &uml; = ¨
    &copy; = ©
    &ordf; = ª
    &laquo; = «
    &not; = ¬
    &shy; = ­
    &reg; = ®
    &macr; = ¯
    &deg; = °
    &plusmn; = ±
    &sup2; = ²
    &sup3; = ³
    &acute; = ´
    &micro; = µ
    &para; = ¶
    &cedil; = ¸
    &sup1; = ¹
    &ordm; = º
    &raquo; = »
    &frac14; = ¼
    &frac12; = ½
    &frac34; = ¾
    &iquest; = ¿
    &times; = ×
    &divide; = ÷
  </body>
</note>

Vim 中,你必须转义括号、大括号和竖线,但是 不是方括号!这显然不太可读。也许 有一些扩展使其更易于使用。刚刚用谷歌搜索 稍微了解了一下,发现Vim 中兼容 Perl 的正则表达式

© www.soinside.com 2019 - 2024. All rights reserved.