[posted & mailed]

On Feb 7, Tobias Reif said:

>>  Well, it depend what you want to do but try it with
>> 
>>    "aaa".consists_of "abc"
>>    "xxxxxxabcxxxxx".consists_of "abc"
>
>Yep; already (hopefully) fixed that bug:
>
>class String
>   def consists_of word
>     self =~ /\A[#{word}]{#{word.length}}\z/
>   end
>end

I'm sorry, but this is a problem that is not easily solved with a regex
(trust me -- I've tried).  The problem with your latest regex (apart from
any quirks that appear in `word' itself that could mess with the PHYSICAL
regex) is that, for "japhy", it merely requires a string of 5 characters
all from the class [japhy].  That means "jjjjj" passes, as does "phyhp".

To use a regex would require such terrible effort as to render it useless
for any real application.  In fact, in light of a previous thread, I don't
think Ruby is capable of it yet, since the Perl solution I can think of
uses (??{ ... }) to dynamically produce the regex.

  $consists_of = make_consists_of("abacus");
  /$consists_of/ && print "$_: ok\n"
    for qw( aabcsu cusbab );

  # in Ruby, that'd be
  # con_of = make_consists_of("abacus")
  # [w% aabcsu cusbab %].
  #   filter{|s| s =~ con_of}.
  #     each{|s| puts "#{s}: ok" }

  sub make_consists_of {
    my $str = shift;
    my $rep = length($str) - 1;

    return qr{
      ^
      (?{ local $s = $str })
      ( [\Q$str\E] )  # \Q...\E ensures metachars are escaped
      (
        (??{
          substr($s, index($s, $+), 1, "");  # remove char from $s
          "[\Q$s\E]"
        )
      ){$rep}
      \z
    }x;
  }

What that does is create a machine that matches any of the allowed
characters, and then removes that character from the string containing the
characters allowed to be matched.

I'm sure the effort involved in doing this is far too great to consider
this solution seriously.  The sort_bytes() method seems far more
understandable, too. ;)

-- 
Jeff "japhy" Pinyan      japhy / pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.