Thursday, August 18, 2011

Re: Matches with zero width if the preceding atom does NOT match at the current position.

On Fri, 19 Aug 2011, baumann Pan wrote:

> Thanks! still confused, could you explain more? or more examples? Thanks again!
>
> On Fri, Aug 19, 2011 at 4:40 AM, Ben Fritz wrote:
>>
>>
>> On Aug 18, 11:02 am, baumann Pan wrote:
>>> Hi Gurus,
>>>
>>> I could not understand the descriptions below about the usage of \@!.
>>>  "a.\{-}p\@!" will match any
>>>         "a", "ap", "aap", etc. that isn't followed by a "p", because the "."
>>>         can match a "p" and "p\@!" doesn't match after that.
>>>
>>> I know why "a"  matches the pattern. but I don't understand why "ap"
>>> could match the pattern "a.\{-}p\@!", so does "aap".
>>> why "appppp" also matches the pattern?
>>>
>>> from the pattern "a.\{-}p\@!", I can tell:
>>> if the pattern is a.{-}p, it  will match ap, aaaap and
>>> absdfasdfasdasdfasdp since .\{-} could be 0 to more chars as few as
>>> possible,but followed with a p.
>>> if\@! is after p, does it mean p should not appear at the end?

No, it means that p should not appear *after* the end.


>>> why ap is ok? adsfasdasdfap appp is matched the pattern
>>> "a.\{-}p\@!"?????
>>>
>>
>> 'a' matches because .\{-} could be zero characters
>> 'ap' matches because .\{-} matches the p,
> I don't think .\{-} will p, since \{-} is matching as few as possible.
>
>> and then the next character
>> is not a p, matching the p\@! with zero width. End-of-line is also
>> "not a p" so that matches as well.
>>
>> The root of the problem is the use of the '.' character which also
>> matches the p.
> why . matches p? I don't understand since . is followed by \{-}, which
> mean as few as possible,
> so I think a.\{-} will only a, aa, aa, aaa, etc.

It sounds like you're misinterpreting "as few as possible". It means
that it will match as few as possible while still:

1. matching as early in the string as possible

2. succeeding, if it can

It really just means that, if it has the option, it will stop at the
first match instead of keeping going until it finds the longest match.

Example of #1:
:echo matchstr('aab','a\{-}b')
echoes 'aab', not 'ab', because 'aab' starts earlier in the string than 'ab'.
(even though 'ab' has fewer 'a's)

Example of #2 is:
:echo matchstr('acaab','a\{-}b')
echoes 'aab', even though 'a' has fewer 'a's, because 'ac' doesn't match

Combining that with \@!, you have the pattern above:
a.\{-}p\@!

Input string:

appp
╵ - a=a, ''=.\{-}, but followed by p, so no match
└┘ - a=a, p=.\{-}, but followed by p, so no match
└─┘ - a=a, pp=.\{-}, but followed by p, so no match
└──┘ - a=a, ppp=.\{-}, not followed by p (followed by nothing), so match

--
Best,
Ben H

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: