How To find all vowels in a string

How To find all vowels in a string

Page 1 of 1

1 Replies - 4010 Views - Last Post: 26 July 2009 - 11:36 AM

#1 saikat_hp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-July 09

How To find all vowels in a string

Posted 26 July 2009 - 03:42 AM

Hi,

I have a question regarding How To find all vowels in a string along with match and print them.[code]
#!/usr/bin/perl

@String="Saikat";
$pattern='[aeiouAEIOU]';

foreach (@String)
{
while (/$pattern/)
{

print"Matched $_:$1 \n";
$_ = $2;


}
}
I am looking for the solution.
please check the above code and help me out.

Thanks all.
:)

Is This A Good Question/Topic? 0
  • +

Replies To: How To find all vowels in a string

#2 KevinADC  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: How To find all vowels in a string

Posted 26 July 2009 - 11:36 AM

If you want to capture patterns you have to include parentheses in the regexp and if you want to find them all in a "while" condition you must add the "g" option to the regexp otherwise you risk creating a condition with no terminating condition (like an infinite loop):

#!/usr/bin/perl

@String="Saikat";
$pattern='[aeiouAEIOU]';

foreach (@String){
   while (/($pattern)/g){
	  print"Matched $_:$1 \n";
   }
}



In the future (if there is any) use the code tags and properly indented code.

This post has been edited by KevinADC: 26 July 2009 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1