I don't know how to transform / the equivalent of this negative lookahead search on Neovim.
&(?!(?:apos|quot|[gl]t|amp);|#)
When I try silver search, it is working. I want to search but only on the single file using /
I don't know how to transform / the equivalent of this negative lookahead search on Neovim.
&(?!(?:apos|quot|[gl]t|amp);|#)
When I try silver search, it is working. I want to search but only on the single file using /
Copyright © 2021 Jogjafile Inc.
Your question is interesting and I effectively have often troubles with the syntax of regular expressions in Vim or other tools that don't use the PCRE or common syntaxes!
Googling a bit, I found this article about lookarounds in Vim.
As you can see, it's just a matter of syntax, again!
A negative lookahead such as
(?!amp;)should be written\(amp;\)\@!.This leads to something like this:
I match
&,'with&\w{2,6};in PCRE, which becomes&\w\{2,6\};in Vim's syntax.Tested that on this XML:
In Vim, you have to escape parenthesis, braces and pipes but not square brackets! This is clearly not very readable. Perhaps there are some extensions to make it easier to use. Just Googled a bit and found Perl compatible regular expressions in Vim.
I've started writing myself a note about the flavours of regular expression engines. It might be useful for others:
..**+\+?\?^^$${3}\{3\}{3,}\{3,\}(regexp)\(regexp\)[abc][abc][^abc][^abc]\2\2(?: )(?=this-after)\(this-after\)\@=Vim✔️, sed❌(?!not-this-after)\(not-this-after\)@!Vim✔️, sed❌(?<=this-before)\(this-before\)@<=Vim✔️, sed❌(?<!not-this-before)\(not-this-before\)@<!Vim✔️, sed❌It seems that sed doesn't handle lookarounds, but the syntax is very similar to Vim for most of the other cases.
Thanks to Friedrich's comment, Vim has helpful patterns to define the start and end of a match:
\zsand\ze.You can place
\zsanywhere in the search, Vim will only match after the start. You can use both to say "find this specific pattern and only replace a part of it". Example with this text:If you only want to uppercase "James" followed by " Bond" or " Cameron":
But if you need negative lookarounds, then it might be more complicated to write the pattern this way, as you'll probably have to use negative character classes. In this case, I would use the negative lookarounds to make the pattern more readable. Typically, to uppercase all "James" which aren't followed by " Tartempion":
Using Perl inside Vim
If Vim is installed with the Perl extension (my case out of the box in Cygwin and Ubuntu), then you can simply use PCRE regular expressions in Vim, typically for your problem of ampersands that need to be converted to HTML entities:
And for the "James" not followed by " Tartempion" example: