2

I have a quick question about the regex (neo)vim uses. After long hours of toiling, staring at documentation and trying everything else I could think of, I seem to be stuck.

What I'm trying to accomplish is this:

I'm working on updating the d.vim file, as there hasn't been an update to it since 2016 (AFAIK, anyways), and I don't know about you, but proper syntax highlighting is one of the great pleasures in my life.

I've been able to brute force vim regex in to my brain to figure out how to match everything I've wanted to up to this point, that point being trying to match the namespace of a function. For example, say I have the following chunk code:

void main() {
    int i = 10;
    i += 2;
    std.writeln(i);
}

The thing I want to return is specifically:

std

To accomplish this, and after (admittedly many hours of) trial and error, I've come up with the following chunk of regex:

\v((^\W{-})@<=\w+)\@=(\.)

Which returns:

std.

While I want it to only return:

std

I've searched high and low, far and wide, and yet come up empty handed. Is it possible to do this? Is there some way to trim the last character off of the end of a regex match? Please teach me, a mere novice, the ways of the RegEx!

Side question that doesn't take priority: I did manage to figure out a string in regular regex that would return what I'm looking for:

(?<=\s)\w+?(?=\.)

My totally-2nd-priority-question here would be: Is there some "easy" way to convert between "regular" regex and vim regex?

Thanks!

[ EDIT: Grammar 'n stuff ]

  • Does that work? \%(\%(^\s\+\)\@<=\w\+\)\ze\.? – Christian Brabandt Mar 8 at 6:45
  • Unfortunately, that one did not work for me. The one from D. Ben Knoble did, though! Thanks for the effort anyways! – Evelyn Mar 8 at 7:03
  • strange, that matched exactly your example for me. – Christian Brabandt Mar 8 at 7:04
  • Outside of substituting entirely different symbols for some of the atoms (mostly the advanced stuff like backrefs) there's not much difference between Perl and Vim very-magic regexes. And anyways all you have to do is look in help docs: :h perl-patterns. – B Layer Apr 14 at 7:27
1

For a stab at your first question, I’d use the match start and end modifiers (\zs, \ze):

\v\zs((^\W{-})@<=\w+)\ze\.

For the second, it’s worth learning vim regex proper. Most of it makes sense, and the more complex features I don’t often need in day-to-day scenarios. Those are also well documented, and accessible when needed.

Finally, no, I am not aware of any tools to convert between vim regex and any other form (for lack of a standard, take, e.g., perl). That said, it gives me ideas for another tool to build...

  • That did the trick! I'm definitely going to be delving more in to regex in the near future. I'm slowly but surely gearing up to going to college for CompSci, and regex is already incredibly helpful. Thanks again! – Evelyn Mar 8 at 7:00
  • 1
    I think you don't need the \@= since \ze will already end the match – Christian Brabandt Mar 8 at 9:10
  • That indeed appears to be the case! – Evelyn Mar 8 at 9:54
  • @Evelyn regex is useful, but not a silver bullet. Best wishes on your journey to university! – D. Ben Knoble Mar 8 at 14:17
  • 1
    It would indeed be wonderful if perl-to-vim regex were to be possible. It gets quite confusing when messing with nomagic, magic and very magic modes. – klaus Mar 8 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.