lookarounds positive negative lookbehind lookahead
Asked Answered
R

3

12

after searching all over google for vsvim lookahead or lookbehind and on the wiki I can't seem to figure out how, or if it even supports lookahead or lookbehind (positives or negatives) and how to use them if so.

I've tried a few different syntaxes like \ze \@= (?<=let \w\+)( \(?<=let \w\+\)( that I've seen on vim answers but none of them seem to be working in vs vim for matching (nor substitution)

how do you do lookarounds in VsVim?

Riella answered 11/5, 2016 at 15:10 Comment(3)
is this what you're looking for? linkMoriah
have not tested, but yes that appears to be what I wantRiella
great, hopefully it works. Let me know.Moriah
M
4

For any newcomers, I'll copy the contents of this link here for the future:

http://ssiaf.blogspot.ru/2009/07/negative-lookbehind-in-vim.html

/\(Start\)\@<!Date

This will match the 'Date' in 'EndDate' and 'YesterdaysDate' but will not match 'StartDate'

/Start\(Date\)\@!

will match the 'Start' in 'Starting but not in 'StartDate'

/Start\(Date\)\@=

will match the 'Start' in 'StartDate' but not in 'Starting

/\(Start\)\@<=Date

will match the 'Date' in 'StartDate' but not in 'EndDate' and 'YesterdaysDate'

Moriah answered 14/10, 2016 at 0:8 Comment(1)
Probably works in Vim, but does not work in VsVim v.2.1.1.0 (when using Visual Studio 2015)Cwm
S
1

I want to expand on the excellent answer from @briansrls. I was looking for a more robust solution that could handle multi-word phrases, wildcards (for potential gaps between phrases) and alternatives (i.e., patterns):

Without wildcards:

  Positive Lookahead:  \(find this\)\(followed by this\|or that\)\@=
  Negative Lookahead:  \(find this\)\(not followed by this\|or that\)\@!
  Positive Lookbehind: \(preceded by this\|or that\)\@<=\(find this\)
  Negative Lookbehind: \(not preceded by this\|or that\)\@<!\(find this\)

With wildcards:

  Positive lookahead:  \(find this\)\(.*\(eventually followed by this\|or that\)\)\@=
  Negative lookahead:  \(find this\)\(.*\(not eventually followed by this:\|or that\)\)\@!
  Positive lookbehind: \(\(eventually preceded by this\|or that\).*\)\@<=\(find this\) 
  Negative lookbehind: \(\(not eventually preceded by this\|or that\).*\)\@<!\(find this\)

Note: For the wildcard versions, the extra parentheses are required so that the wildcard is excluded from the alternatives group, but is included in the lookaround group. This prevents duplicating the wildcards for every alternative. One could also use \zs & \ze to avoid the extra parentheses, but I find this method slightly more intuitive.

For more information:

Update: It appears that \zs & \ze are not yet implemented in VsVim as of this time.

Shaving answered 6/8, 2018 at 20:34 Comment(0)
M
0

I use vscode 1.86. I found VsVim (like vscode) uses standard regex syntax, rather than the VIM specific syntax. For example for lookaround (taking the reference from https://www.regular-expressions.info/lookaround.html):

  • (?<!Start)Date will match a "Date" not following "Start"
  • Start(?!Date) will match "Start" not followed by "Date"

The lookahead (or lookbehind) group is not a capturing group, even though it is surrounded by parentheses.

Moseley answered 26/2 at 9:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.