Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

REGEX : a line contains all five vowels 1

Status
Not open for further replies.

whygh

Programmer
Apr 18, 2005
16
CA
Hello everyone,

I want to look for a line containing all five vowels with regular expression. I wrote one, but it is very stupid. do you guys have any good way?

while(<STDIN>){
if ($_!~/\w*a\w*/) {
print "No Vowel a\n";
last;
}
elsif($_!~/\w*e\w*/) {
print "No Vowel e\n";
last;
}
elsif ($_!~/\w*i\w*/) {
print "No Vowel i\n";
last;
}
elsif ($_!~/\w*o\w*/) {
print "No Vowel o\n";
last;
}
elsif ($_!~/\w*u\w*/) {
print "No Vowel u\n";
last;
}
print "$_\n";
}

appreciate any help
 
Regular expressions aren't the way to go for something like that (the \w* on either side doesn't add anything). You'd be best off checking with index() five times for maximum efficiency.

Or, if you're dealing with a large amount of data at a single shot and this is just an example of your final method, you could study() the line first, then run five short regex checks on it for each literal string. That can be all kinds of quick where more linear options take forever.

________________________________________
Andrew

I work for a gift card company!
 
not stupid at all, it all depends on what exactly you want to do:

Code:
while(<STDIN>){
   next unless (/a/ && /e/ && /i/ && /o/ && /u/);
   print "$_\n";
}
 
I agree with icrf that using index() would be better, especially if there is a lot of text.
 
Thanks your reply. indeed, \w* is no use here.

actually, I am learning regular expression. I got this exercises from the book <<O'REILLY Learning Perl>>.
it is the exercises after the chapter "Regular Expressions" .
a. looks for a line containing all file vowels.
b. five vowels have to be in order and intervening letters donot matter.
c. all vowels must be in an increasing orer. no "e" can occur before an "a".

So, I think it must be the better way to implement the exercises.
 
from KevinADC try

Code:
while(<STDIN>){
   next unless (/a.*?e.*?i.*?o.*?u/i);
   print "$_\n";
}


Michael Libeson
 
mlibeson,

The only caveat with that solution is that the vowels must be in that order: aeiou

 
mlibeson,

I missed this:

b. five vowels have to be in order and intervening letters donot matter.

A star for you to make up for my inability to read.


 
Hi ,

Actually, I donot know the difference between "b)" and "c)".
 
reading (c) literally, you'd need something like
Code:
/^[^eiou]*a[^iou]*e[^ou]*i[^u]*o.*u/
to make sure that no "e" occurs before the first "a" etc. If they mean no "e" before any "a", then it's
Code:
/^[^eiou]*a[^aiou]*e[^aeou]*i[^aeiu]*o[^aeio]*u[^aeio]*$/

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top