3

I want to select/visualize a regexp GROUP in a vim regexp expression in vim.

By example I would like to highlight (search) a span of characters inside square brackets, having this line:

bla bla bla [Corso De Stefanis 88 interno 4](address) blabla

I would like to just select this sequence of chars:

Corso De Stefanis 88 interno 4

So I tried the regexp

/\[\(.\{-}\)\]

Where the aim is to have the regexp GROUP \(.\{-}\) contained in sqare brackets. But in this case I select the pattern including square brackets (see screeenshot).

And if I specify the referenced group with \1, the search fails:

/\[\(.\{-}\)\]\1

Where I'm wrong?

enter image description here

1 Answer 1

4

reading here: exclude character classes inside [] in a regex

and here: exclude character classes inside [] in a regex

I find the solution, that's:

/\[\zs.\{-}\ze\]

In a sense, capture group seems not working, but you can select the "group" preceding sequence by \zs and finishing with \ze. vim regexp syntax is weird for me.

2
  • 1
    Try :help pattern. Searches can include capture groups, but not backreferences (the \1, etc.). \zs and \ze set boundaries for the final "match." Backreferences are still used in substitution commands, however (:help :substitute and :help sub-replace-special).
    – D. Ben Knoble
    Commented Mar 30, 2022 at 14:16
  • Thanks. BTW, My need is related to my vim plugin Highlight.vim. See example s and screenshots here: github.com/solyarisoftware/Highlight.vim/tree/master/… Commented Mar 30, 2022 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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