Hacker News new | past | comments | ask | show | jobs | submit login

vim.

This one bugs me, because it's a cool enough trick, and I want to expand my thought process when it comes to regex.

The closest I can get visually in vim:

  /"tarzan"\zs\|tarzan
(The \zs flag starts the cursor and highlighting at a given location inside of a larger regex match. I didn't use a capture group here because it didn't help.)

Two problems:

1. This will still match the quoted word when pressing "n" but mostly unhighlights it. (see next point)

2. Whatever single character is after the unwanted match is highlighted, so this would only help for visually searching for reasonably long expressions.

-----

An alternative that I would use unless a special edge case was present (and this is basically the dumb version of the author's typical solutions):

  /tarzan\ze[^"]
(\ze ends the match but continues to filter whatever follows)

In a persistent edge case, I'd probably resort to macros or temporary replacement of the unwanted term. But that's not very satisfying, is it?

-----

More details:

Capture group references evidently work in vim's search mode. I hadn't tried until now. I only see utility in a few cases e.g. finding any duplicate word. The specific case given at the link does not work as-is. I'd need a way of evaluating the author's full expression and then only match the capture group. Is there a way to put the capture group \1 outside of the alternation?

There's possibly a way to use back-referencing or global search and execution or branches. The solution is also probably very clever and concise! I've tried a few permutations and am still stumped.

-----

Last best attempt:

  /"\@<!tarzan"\@<!
(\@<! will match if the previous atom---in this case, double quotes---is not present.)

An edge case where this falls apart? Single leading double quote e.g. "tarzan

Is there a better way?




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: