0

Engine: Vim RegEx

Input:

\begin{thoerm}\label{ABC_for_all}
    If the ABC is a common good, $ABC(places)$ includes everything.
\end{thorem}

We will now prove Threorem \ref{ABC_for_all}.

Expression:

(?<!(\\label|\\ref)\{\w*)ABC(?!\w*\})

Expected:

I want to match (and replace in neovim) "ABC", but only if it is not part of a name (so not in a \label{...} or \ref{...} context.

Result:

Doesn't work.

I know how to replace it if it occurs within the context:

:s/(\\label\{\w*)ABC(\w\})*/\1alphaber\2/g

But not the occurrences outside of it.

Lookahead didn't work, because the ABC may occur somewhere within the label command, thus I need variable width. I also was unable to figure out how to turn a capture group into the actual match. It seems like such a common case, but everything I found so far didn't include it.

halfer
  • 19,824
  • 17
  • 99
  • 186
grbll
  • 31
  • 5
  • Regular expressions are not good at checking context like this. A real parse is a better choice. This looks like TeX, it shouldn't be hard to find a TeX parser. – Barmar Aug 02 '23 at 17:28
  • But the weird thing is, it is quite easy to replace the matches within the context. However, I ramain unable to replace all except them, which seems to be a very dual problem. – grbll Aug 02 '23 at 19:42
  • They're not really duals. It's easy to represent "inside" because you can write `start.*end`. But "outside" is hard because something that's not inside one pair could still be inside another pair. – Barmar Aug 02 '23 at 19:46
  • you can use a lookahead to match a string that doesn't include the start/end patterns, but that will still match something inside. – Barmar Aug 02 '23 at 19:47
  • When the context is delimited by specific bracketing characters, like `[...]` you can use the aproaches here: https://stackoverflow.com/questions/17284947/regex-to-get-all-text-outside-of-brackets but that doesn't work when it's bracketed by keywords. – Barmar Aug 02 '23 at 19:49

1 Answers1

0

Since I was told this in not possible with regex, my workaround was as follows:

  1. Rename everyhting within the specified context by some MARKER.
  2. Replace everything else.
  3. Replace the MARKER by the original.

Each time using regex.

grbll
  • 31
  • 5