>  I am curious whether this kind of meta-regexp is
>  possible with vim:
> 
>  I want to match a certain kind of pattern and want to
>  do "something" with it.
>  
>  The kind of pattern does not describe a group of chars
>  but their relation to each other.
> 
>  Example:
>  I want to search for a number sequence like
>  1221
>  and also
>  2332
>  and also
>  3443
>  and also....
> 
>  Or:
>  I want to find a sequence of five identical characters. The 
>  character itsself doesn't matter.

I think the best "recipe" is knowing the ins and outs of Vim's
regexp syntax.  It's possible to describe all the cases you
describe using "\(...\)" tagging and back-references, depending
on the flexibility of your description.  However, there's no
meta-character syntax for sequence-ness.  Thus, it's easy to find

        digitA digitB digitB digitA

patterns with

        \(\d\)\(\d\)\2\1

but considerably more complex to find

        digitA digitA+1 digitA+1 digitA

where the 2nd/3rd characters are algorithmically related to the
1st/4th characters.  There are also edge cases about "what should
X be for '9XX9'".  If it was sufficiently complex to find
something like this, I'd build the regexp with something like
this untested

        :let x=''
        :let i=0
        :while (i<9)| let x=x.i.(i+1).(i+1).i.'\|' | endwhile
        :let x=x.'9XX9'

which would yield something like

        0110\|1221\|2332\|....\|8998\|9XX9

in register "x" which could then be dumped into a search using
control+R followed by "x".  One might even do the above algorithm
directly on "@/" rather than on x.

For your second case, it's as easy as

        \(\a\)\1\{4}

(that's a digit "one" not a letter "ell" or "eye")

For your first case, I've occasionally thought it would be quite
handy to have an expression-register in the search as well as in
the replacement (akin to the "\=" in the replacement) that could
operate with access to its context, something like

        /\(\d\)\=((submatch(1)+1)%10)\=((submatch(1)+1)%10)\\1

using an imaginary \=()  syntax for expressions.  I haven't
thought long and hard about it; I don't know if it would have
horrible performance; I don't know if it would have other
breakages.  But it would be darn powerful. :)

The above would use '9009' as without the modulus ("%"), it would
search for "910109"

Alas, no such atrocity exists yet, though you can bet that if it
did show up, I'd be abusing it left and right :*)

-tim




Reply via email to