in reply to Re: Re: Re: $_ works, $my_variable doesn't?
in thread $_ works, $my_variable doesn't?

my @strings = qw( aaaaa eeeee iiiii abcde aeiou bdfhj ); my %regexes = ( alternation => qr/a|A|e|E|i|I|o|O|u|U/, class => qr/[AaEeIiOoUu]/ ); foreach my $string (@strings) { print "$string:\n"; while (my ($k, $r) = each %regexes) { print "\t$k - " . ($string =~ /$r/ ? 'YES' : 'NO') . $/; } } --------------- aaaaa: alternation - YES class - YES eeeee: alternation - YES class - YES iiiii: alternation - YES class - YES abcde: alternation - YES class - YES aeiou: alternation - YES class - YES bdfhj: alternation - NO class - NO

Please give me a counter-case. As far as I can tell, the two regexes are identical, save that the alternation one is slower.

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: $_ works, $my_variable doesn't?
by duff (Parson) on Mar 30, 2004 at 14:28 UTC
    The OP was looking for strings that contain all of the vowels. Your code doesn't do that.
      In my defense, neither did the code I was referring to.

      That is an interesting question - is there a way to have a regex take a character class and determine if the string contains all the characters in the class?

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        Yes, the original mistake was mine. I have since updated that node. I don't know any slick way of putting it all in one character class, though with lookahead it could be wedged into one regex.

        Update: here's my first shot at a lookahead version:

        my @strings = qw( aaaaa eeeee iiiii abcde aeiou bdfhj ); my $range = 'AaEeIiOoUu'; my $alt = join '|', split //, $range; my %regexes = ( alternation => qr/$alt/, class => qr/[$range]/, lookahead => qr/([$range])((?!\1)[$range])((?!\2)[$range])((?!\3 +)[$range])((?!\4)[$range])/, ); foreach my $string (@strings) { print "$string:\n"; while (my ($k, $r) = each %regexes) { print "\t$k - " . ($string =~ /$r/ ? 'YES' : 'NO') . $/; } }

        And the output:

        aaaaa: alternation - YES class - YES lookahead - NO eeeee: alternation - YES class - YES lookahead - NO iiiii: alternation - YES class - YES lookahead - NO abcde: alternation - YES class - YES lookahead - NO aeiou: alternation - YES class - YES lookahead - YES bdfhj: alternation - NO class - NO lookahead - NO