Wednesday, October 3, 2012

Re: Problem with a regular expression in Vim

On Tue, Oct 02, 2012 at 10:37:34PM EDT, 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. However /==[^=]\{-}==/ does match the
> "aaa" and "ddd" WITH the pair of "==". I got lost here.

If I understand the question correctly, this is similar to the textbook
problem of matching strings between double quotes:

xxx "aaa" bbbccc "ddd" yyy

If you want to match "aaa" and then "ddd", the solution is something
like:

/"[^"]*"

You need the negated character class to prevent the regex engine from
matching the entire - "aaa" bbbccc "ddd" - string in one big gulp.

Now, at least in Vim, if you want the quotes *not* to be part of the
match, you would be tempted to try:

/"\zs[^"]*\ze"

The problem is that this doesn't do what we hoped for.

The reason, I think, is that if you specify a zero-length match for your
delimiter (double quotes or a pair of equal signs), once a match is
completed, the regex engine does not consider that the ending delimiter
has been consumed, and hence, when looking for the next match, it starts
over where it left off..

So, it will match:

aaa
bbbccc .. that's space+bbbccc+space
ddd

.. which is not what you want: you only want to match aaa and bbb
because although ' bbbccc ' is immediately preceded and followed by
a double quote, it is obviously *not* a quoted string.

Have I understood you correctly..?

I'm not sure there's a way to specify a zero-length match *and* cause
the regex engine to consume the matched characters before it moves to
attempt the next match, but I wouldn't be too optimistic.. because it
feels like you would be asking the regex engine to do two things that
are contradictory to each other.

If you're doing substitutions, could a possible workaround be not to
bother about the zero-length matches in the first place and.. "add them
back" so-to-speak as part of the substitution string..?

| xxx==aaa==bbbccc==ddd==yyy
|
| :%s/==[^=]==/==XXX==/g

.. where aaa and ddd would be replaced by XXX..

CJ

--
Focus follow mouse users will burn in hell!!!

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