1

consider this sentence (a line in a text file):

 mi chiamo [Giorgio Robino](person) ed abito in [corso Magenta 35/4 a Genova](address)

I want to search (select/extract) the pattern <any chars> inside [<any chars>](<word>).

By example, from text: [Giorgio Robino](person), I want to select Giorgio Robino

And from text: [corso Magenta 35/4 a Genova](address), I want to select corso Magenta 35/4 a Genova.


I entered these 2 regexp search patterns:

/\[\zs.\{-}\ze\](person)
/\[\zs.\{-}\ze\](address)

But, as you can see in the screenshot, it happens that the second search highlights a single selection, as the span:

mi chiamo [Giorgio Robino](person) ed abito in [corso Magenta 35/4 a Genova](address)
           ^                                                              ^ 
           |                                                              |

Instead I would like to have 2 separated selections:

mi chiamo [Giorgio Robino](person) ed abito in [corso Magenta 35/4 a Genova](address)
           ^            ^                       ^                         ^ 
           | selection1 |                       | selection2              |

So my regexp fails, being "non-greedy".

May you help me finding the right regexp? Any solution suggested?

BTW, my question is related to the plugin I created: https://github.com/solyarisoftware/Highlight.vim

enter image description here

6
  • 2
    \[\zs[^\[\]]\{-}\ze\](address)
    – Maxim Kim
    Apr 8, 2022 at 10:39
  • 1
    or \[\zs.\{-}\ze\](.\{-})
    – Maxim Kim
    Apr 8, 2022 at 10:40
  • AH! I understand that your first regexp extends my own, excluding chars [ and ] and so relating my plughi Highligt.vim I solve with :HighlightText \[\zs[^\[\]]\{-}\ze\](person) and :HighlightText \[\zs[^\[\]]\{-}\ze\](address). Thanks Apr 8, 2022 at 11:13
  • Try to match anything until a closing ], Apr 8, 2022 at 11:19
  • Thanks to @MaximKim : github.com/solyarisoftware/Highlight.vim#thanks Apr 8, 2022 at 12:40

1 Answer 1

1

I add the answer following @D. Ben Knoble invitation. What solved is the @Maxim Kim first comment!

The solving regexp is:

\[\zs[^\[\]]\{-}\ze\](address)

The trick is to exclude chars [ and ]. So, relating my plugin Highligt.vim, I solved with

:HighlightText \[\zs[^\[\]]\{-}\ze\](person) 

and

:HighlightText \[\zs[^\[\]]\{-}\ze\](address)

Special thanks.

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.