I have to analyze a log file and I am looking for repeated patterns, for example i want to find:
Start ...
some lines ...
Start ...
So for this I am using
/Start\_.\{-}Start
but unfortunately in finds also this, which I want to avoid:
Start ...
End ...
Start ...
CodePudding user response:
Try with Start\(\_.\(End\)\@!\)\{-}Start
.
I've basically just changed your \_.
to something more complex, namely \(\_.\(End\)\@!\)
, which matches \_.
where End
does not match.
You can even enforce that the End
must be at the beginning of the line for it to prevent the match, by just changing End
to ^End
in the regex above.
Notice, however, that if you truly have the same word for opening and closing the group you want to match, then the regex will match even between groups. For instance, in this
Start ... here
some lines ...
Start ... here
Start ... here
not End...
Start ... here
Start ...
End...
Start ...
the regex Start\(\_.\(^End\)\@!\)\{-}Start
will match on the four lines marked as here
.
So if you can write a regex re1
which only matches the opening, one regex re3
that matches only the closing, and one regex re2
that matches what you don't want to match, then you can combine the into re1\(\_.\(re2\)\@!\)\{-}re3
.
A deleted answer by Andreas Louv used \%(…\)
instead of \(…\)
. As you can see in :help E53
,
- that's for not counting it as a subexpression (i.e. successive
\(
s correspond to\n
for increasingn
, but\%(
s don't have a corresponding number through which you can refer to them; - and it's a little bit faster.