Matching a pattern NOT containing another pattern

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Matching a pattern NOT containing another pattern

Dave Roberts
Is it possible to match a pattern only if it does not contain another
specified pattern anywhere in it?

For example, match a 'foo' followed by a 'bar' as long as there is no
'dave' between the foo and bar.

So this foo line matches bar according to dave  (no embedded dave)
but this foo line, according to dave, doesn't match bar. (embedded dave)

It's easy enough to match if the pattern is in a specific place but how
to match (or not) if the pattern can be anywhere?

Apologies if this is described right in front of me in "pattern.txt" -
it's getting late...

Thanks,

- Dave
Reply | Threaded
Open this post in threaded view
|

Re: Matching a pattern NOT containing another pattern

iler.ml
On 3/30/06, Dave Roberts <[hidden email]> wrote:

> Is it possible to match a pattern only if it does not contain another
> specified pattern anywhere in it?
>
> For example, match a 'foo' followed by a 'bar' as long as there is no
> 'dave' between the foo and bar.
>
> So this foo line matches bar according to dave  (no embedded dave)
> but this foo line, according to dave, doesn't match bar. (embedded dave)
>
> It's easy enough to match if the pattern is in a specific place but how
> to match (or not) if the pattern can be anywhere?
>
> Apologies if this is described right in front of me in "pattern.txt" -
> it's getting late...

:he /\@!

Yakov
Reply | Threaded
Open this post in threaded view
|

Re: Matching a pattern NOT containing another pattern

Dave Roberts
Yakov Lerner wrote:

> On 3/30/06, Dave Roberts <[hidden email]> wrote:
>  
>> Is it possible to match a pattern only if it does not contain another
>> specified pattern anywhere in it?
>>
>> For example, match a 'foo' followed by a 'bar' as long as there is no
>> 'dave' between the foo and bar.
>>
>> So this foo line matches bar according to dave  (no embedded dave)
>> but this foo line, according to dave, doesn't match bar. (embedded dave)
>>
>> It's easy enough to match if the pattern is in a specific place but how
>> to match (or not) if the pattern can be anywhere?
>>
>> Apologies if this is described right in front of me in "pattern.txt" -
>> it's getting late...
>>    
>
> :he /\@!
>
> Yakov
>
>  

According to the doc, it matches if the preceding atom does not match at
the *current* position. In other words, I can use it to not match
"foodavebar" but not "foo then davebar". Right?

Thanks,

- Dave
Reply | Threaded
Open this post in threaded view
|

Re: Matching a pattern NOT containing another pattern

Dave Roberts
Dave Roberts wrote:

> Yakov Lerner wrote:
> ...
> According to the doc, it matches if the preceding atom does not match
> at the *current* position. In other words, I can use it to not match
> "foodavebar" but not "foo then davebar". Right?
>
> Thanks,
>
> - Dave
>

I was wrong - I see it now - tricky!

Thanks again,

- Dave
Reply | Threaded
Open this post in threaded view
|

Re: Matching a pattern NOT containing another pattern

Charles E Campbell Jr
In reply to this post by Dave Roberts
Dave Roberts wrote:

> Is it possible to match a pattern only if it does not contain another
> specified pattern anywhere in it?
>
> For example, match a 'foo' followed by a 'bar' as long as there is no
> 'dave' between the foo and bar.
>
> So this foo line matches bar according to dave  (no embedded dave)
> but this foo line, according to dave, doesn't match bar. (embedded dave)
>
> It's easy enough to match if the pattern is in a specific place but
> how to match (or not) if the pattern can be anywhere?
>
> Apologies if this is described right in front of me in "pattern.txt" -
> it's getting late...


Try LogiPat, http://vim.sourceforge.net/scripts/script.php?script_id=1290 .
It provides !, & and | (logical not, logical and, logical or) operators
and () for grouping.

:LP !"foo.*dave.*bar"

Regards,
Chip Campbell

Reply | Threaded
Open this post in threaded view
|

Re: Matching a pattern NOT containing another pattern

Gerald Lai-2
In reply to this post by Dave Roberts
On Thu, 30 Mar 2006, Dave Roberts wrote:

> Is it possible to match a pattern only if it does not contain another
> specified pattern anywhere in it?
>
> For example, match a 'foo' followed by a 'bar' as long as there is no 'dave'
> between the foo and bar.
>
> So this foo line matches bar according to dave  (no embedded dave)
> but this foo line, according to dave, doesn't match bar. (embedded dave)
[snip]

If you are just interested in matching lines (as opposed to characters),
then this regex should do it:

   /^\%(.*foo.*dave.*bar\)\@!.*

See:

   http://www.vim.org/tips/tip.php?tip_id=1180

Then again, if you're looking to, for instance, delete those lines by
means of

   :g/<regex_above>/d

you might as well do

   :v/foo.*dave.*bar/d

See ":help :v".
Hope this helps.
--
Gerald
Reply | Threaded
Open this post in threaded view
|

Re: Matching a pattern NOT containing another pattern

Dave Roberts
In reply to this post by Charles E Campbell Jr
Charles E Campbell Jr wrote:

> Dave Roberts wrote:
>
>> Is it possible to match a pattern only if it does not contain another
>> specified pattern anywhere in it?
>>
>> For example, match a 'foo' followed by a 'bar' as long as there is no
>> 'dave' between the foo and bar.
>>
>> So this foo line matches bar according to dave  (no embedded dave)
>> but this foo line, according to dave, doesn't match bar. (embedded dave)
>>
>> It's easy enough to match if the pattern is in a specific place but
>> how to match (or not) if the pattern can be anywhere?
>>
>> Apologies if this is described right in front of me in "pattern.txt"
>> - it's getting late...
>
>
> Try LogiPat,
> http://vim.sourceforge.net/scripts/script.php?script_id=1290 .
> It provides !, & and | (logical not, logical and, logical or)
> operators and () for grouping.
>
> :LP !"foo.*dave.*bar"
>
> Regards,
> Chip Campbell
>

I've looked at LogiPat before and it's useful but I'm looking at these
regular expressions to figure out how to do syntax coloring on a
complicated multi-line pattern that can have the same start and end
patterns but differ by embedded patterns that mean something different
if they themselves have a match or not inside the pattern. I'm getting
closer using \@! but am still having trouble in one place.

If I can't figure it out I'll post the whole thing eventually to let
someone else have a crack at at.

Thanks,

- Dave
Reply | Threaded
Open this post in threaded view
|

Re: Matching a pattern NOT containing another pattern

Hari Krishna Dara
In reply to this post by Dave Roberts

On Fri, 31 Mar 2006 at 7:13am, Dave Roberts wrote:

> Charles E Campbell Jr wrote:
> > Dave Roberts wrote:
> >
> >> Is it possible to match a pattern only if it does not contain another
> >> specified pattern anywhere in it?
> >>
> >> For example, match a 'foo' followed by a 'bar' as long as there is no
> >> 'dave' between the foo and bar.
> >>
> >> So this foo line matches bar according to dave  (no embedded dave)
> >> but this foo line, according to dave, doesn't match bar. (embedded dave)
> >>
> >> It's easy enough to match if the pattern is in a specific place but
> >> how to match (or not) if the pattern can be anywhere?
> >>
> >> Apologies if this is described right in front of me in "pattern.txt"
> >> - it's getting late...
> >
> >
> > Try LogiPat,
> > http://vim.sourceforge.net/scripts/script.php?script_id=1290 .
> > It provides !, & and | (logical not, logical and, logical or)
> > operators and () for grouping.
> >
> > :LP !"foo.*dave.*bar"
> >
> > Regards,
> > Chip Campbell
> >
>
> I've looked at LogiPat before and it's useful but I'm looking at these
> regular expressions to figure out how to do syntax coloring on a
> complicated multi-line pattern that can have the same start and end
> patterns but differ by embedded patterns that mean something different
> if they themselves have a match or not inside the pattern. I'm getting
> closer using \@! but am still having trouble in one place.
>
> If I can't figure it out I'll post the whole thing eventually to let
> someone else have a crack at at.
>
> Thanks,
>
> - Dave

The way I usually manage with -ve part of the patterns is that I first
search without that and get the matching lines in a separate window, and
further narrow them by remove the lines matching the -ve pattern. This
requires multiple steps, and might not always suit your needs, but it
usually is simple enough. You can use grep to generate errorlist and
work on the error-list window, but I prefer to use my greputils plugin
to redirect the :g/re/p result to a window.

--
HTH,
Hari

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
Reply | Threaded
Open this post in threaded view
|

Re: Matching a pattern NOT containing another pattern

Charles E Campbell Jr
In reply to this post by Dave Roberts
Dave Roberts wrote:

> I've looked at LogiPat before and it's useful but I'm looking at these
> regular expressions to figure out how to do syntax coloring on a
> complicated multi-line pattern that can have the same start and end
> patterns but differ by embedded patterns that mean something different
> if they themselves have a match or not inside the pattern. I'm getting
> closer using \@! but am still having trouble in one place.
>
> If I can't figure it out I'll post the whole thing eventually to let
> someone else have a crack at at.

Well, LogiPat does save its result in the @/ register, but you can also
see what it would produce:

:echo LogiPat('"abc" & "def")
:echo LogiPat('! "pattern")

HTH,
Chip Campbell