Tuesday, October 2, 2012

Re: Problem with a regular expression in Vim

On Tuesday, October 2, 2012 9:38:20 PM UTC-5, Xell Liu wrote:
> Hi all,
>
>
>
> Suppose this text fragment:
>
>
>
> xxx==aaa==bbbccc==ddd==yyy
>
>
>
> How can I match the "aaa" and "ddd" between the pair of "==" without
>
> matching the bbbccc (or, of course, "xxx" or "yyy")? Apparently
>
> /==\zs[^=]\{-}\ze==/ fails.

For me it matches exactly what you told it to:

==\zs : after two '=' characters...
[^=] : match ANY character which is not a '='...
\{-} : ANY number of times...
\ze== : until another pair of '=' characters.

In your example, this matches aaa, bbbccc, and ddd. There is no reason I can think of to expect otherwise.

> However /==[^=]\{-}==/ does match the
>
> "aaa" and "ddd" WITH the pair of "==". I got lost here.
>
>

I'm guessing you noticed that hitting 'n' after searching for this pattern does not match the bbbccc in your example text. This actually surprised me a little, but I think it happens because:

You are searching for the "next" match.
The beginning of the ==bbbccc== string, which matches your PATTERN, is inside the current match.
To be useful, the search needs to start AFTER the current match, therefore your pattern is not even tried at this next position.

By setting match end with \ze in your first pattern, you make it so the current match ends BEFORE the ==bbbccc=== string, therefore allowing your pattern to be tried and matched on this text.

Now, how to fix this? Well...that depends on what you actually want. Somebody suggested aaa\|ddd in your pattern, which will certainly match your example text as desired, but I have a feeling the example text is actually not all that similar to what you actually want to match.

In what way are aaa and ddd similar? How do they differ from bbbccc? Is it a matter of number of characters? If so, just replace \{-} with \{3} to match exactly three characters, or \{1,3} for 1-3 characters, or \{0,3} to more closely match what you have now. Or are you actually looking for a literal aaa or ddd?

--
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:

Post a Comment