0

I made the regex:

/^\n\t(\n|.)*?\n$/gm

(prepended with \v inside Vim) to select consecutively indented lines (with tab) separated by a top blank line.

But while it works in other languages it doesn't seem to do in Vim.

I expect to get the following "underlined" parts highlighted:

A random fact:
_
____this_______
________should_
____be_________
____highlighted
_
Another random fact:
    this
        should
    NOT
    be
    highlighted

But what I get is:

A random fact:
_
____this_______
________should_
____be_________
____highlighted
_
_Another_random_fact:
____this_______
________should_
____NOT________
____be_________
____highlighted

What am I doing wrong?

1
  • Assuming *? is some other regex flavor's "non-greedy *"… don't try to translate other regexes into Vim patterns. Instead, read :help pattern; the syntaxes for some things are quite different.
    – D. Ben Knoble
    Nov 28, 2022 at 15:46

2 Answers 2

0

There is a BUG in the given regex.

The given regex /^\n\t(\n|.)*?\n$/gm is going to match "newline & tab" at the starting , then it is going to match "newline or other character" which works across lines & hence it matches the lines which start with "other character" because that will be Preceded by "newline" !

Solution :
/^\n\t.*(\n\t.*)*\n$/gm
What this regex is trying :
Match "newline & tab" followed by "other characters" , then match "newline & tab" followed by "other characters" , terminating in "newline" not followed by "tab" which is the correct regex.

This will work.

0

I believe the last \n is eaten by the first occurrence

/^\n\t\(\n\|.\){-}\ze^\n/g

Or:

/\v^\n\t(\n|.){-}\ze^\n/g

Or:

/\v^\n\t\_.{-}\ze^\n/g

The {-} replace the * make the repetition non-greedy to avoid that two consecutive blocks are treated like one.

The \ze make the the last \n is not eaten.

The ^\n search for an ending blank line.

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.