2009/5/29 Brian Candler <b.candler / pobox.com>:
> Robert Klemme wrote:
>>>> irb(main):009:0> /^(.)(?!\1)(.)(?!\1|\2)(.)(\2)$/ =~ "abcb"
>>>> => 0
>>>> irb(main):010:0> /^(.)(?!\1)(.)(?!\1|\2)(.)(\2)$/ =~ "abbb"
>>>> => nil
>>
>> The problem with this approach is that it does not work for strings of
>> arbitrary length.  ¨Βφειζ ωοαδκυστ ιτ το χοςλ ζονυμτιπμε μεξητθ>> you always have a fixed upper limit for which it can work.
>
> The OP explicitly said that he wanted to match single characters. It
> would also make sense for other fixed-width fields, or delimited fields.

But he did not state explicitly that the _strings_ that he wanted to
analyze have fixed length.  This is where your approach breaks.  If
his strings are always of maximum length four or he only needs to make
sure the four first characters do not repeat then it will work.
"Four" could easily be any number up to "nine" but even then naming of
groups might cause trouble; in any case the expression will soon look
ugly, especially if strings can be "up to N" characters long.

> With neither fixed sizes nor delimiters, I don't think it makes any
> sense. It would become "match any sequence of characters, followed by
> any sequence of characters which isn't the same as the first sequence of
> characters, followed by any sequence of characters which isn't the first
> or second, followed by the second sequence of characters". And there
> would be a squillion different ways to try and slice the string to make
> it match.

That's beyond the power of regular expressions.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/