On 9/8/11 Thu Sep 8, 2011 8:58 AM, "Marc" <sono...@fannullone.us> scribbled:
> I'm trying to capitalize 3 and 4 letter words that contain only vowels or > consonants, but not both. The code I've come up with is not working > correctly. Given the string 'The Kcl Group', it should return 'The KCL Group' > but it is also capitalizing the word 'THE'. What am I doing wrong? The regular expression m/[aeiouy]+/gi will match a string that contains one or more vowels. Thus, the string 'The' matches and is capitalized because it contains a vowel: 'e'. If you want the regular expression to match only strings that consist entirely of vowels, you need to anchor the pattern to the beginning and end of the string: m/^[aeiouy]+$/i The 'g' modifier is not needed, as you are only attempting to match the entire string, not repetitive substrings within the string. A similar change should be made to the consonant regular expression. > > use strict; > use warnings; > > my $string = 'The Kcl Group'; > > my @words = split(/ /, $string); > my @new_words; > foreach my $word (@words) { > if ((length $word >= 3 and length $word <= 4) and ($word !~ m/[aeiouy]+/gi or > $word !~ m/[bcdfghjklmnpqrstvwxz]+/gi)) { > $word = uc($word); > } > push @new_words, $word; > } > $string = "@new_words"; > > print $string . "\n"; -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/