Thursday, October 4, 2012

Re: [Rephrased] Problem with a regular expression in Vim

On Wed, Oct 3, 2012 at 11:37 PM, Ben Fritz <fritzophrenic@gmail.com> wrote:
> On Wednesday, October 3, 2012 9:05:14 AM UTC-5, Xell Liu wrote:
>> Hi all,
>>
>> Sorry for the previous mail where my ambiguous expression led to a
>>
>> somewhat time-wasting discussion. Thanks for the guys who tried to
>>
>> help. Here is the rephrased version.
>>
>>
>>
>> I what to use command :match to highlight some text, which is
>>
>> free-form and thus can not be enumerated. The text is always
>>
>> surrounded by a pair of "==". I need a regex to match the text.
>>
>>
>>
>> In the following example, what I want to be highlighted is "aaa" and "bbb"
>>
>>
>>
>> xxx==aaa==cccddd==bbb==yyy
>>
>>
>>
>> In the following example, what I want is "a", "c", "e" and "g"
>>
>>
>>
>> x==a==b==c==d==e==f==g==y
>>
>>
>>
>
> Ok...so you want all "even numbered" things surrounded by ==, correct?
>
> So you have:
>
> {beginning of line}{possible text not to match}=={text to match}=={text not to match}=={text to match}==...
>
> This seems to work for me:
>
> \%(^\|[^=]*==[^=]*==\)\@<=[^=]*==\zs[^=]*\ze==
>
> The trick here is that I match only at positions where either the beginning of the line, or a previous non-match/match pair precede the match.
>
> Probably this pattern could be made simpler but I wasn't able to find a simpler one quickly. The /\@<= is special because unlike \zs, as noted in the :help, "the part of the pattern after '\@<='...[is]...checked for a match first", so I couldn't just drop it and use the \zs by itself.
>
> By the way, you still haven't said what task you're trying to accomplish, beyond that you want to use :match. If syntax highlighting with :syn match would work for you instead, probably the easiest way to highlight these would be using the full ==...== string, and pattern offsets. :he :syn-pattern-offset
>
> --

Hi Fritz,

Thanks very much for the solution and, especially, for the shift in
thinking -- I couldn't notice the "even numbered" thing. And your
practical example of using the \@<= teaches me even more.

As to the "task beyond the :match", hmm, I did have something further
and off-topic to do (and I already knew the syn-pattern-offset of
syntax highlight). However, I just saw the regex difficulty as a pure
"technical" challenge and hoped to learn something from it. Now I did
:-)

Best,
Xell

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