Saturday, July 23, 2011

To extract all occurence of word from string perl

#To extract occurence of word from string
#To extract a occurence of word from a string it is very easy in perl.We need to search the occurence in global way.
#For example to extract all occurence of emailid from a string
Content of the perl file:

#!/usr/bin/perl
$dd='karthik karthik@gmail.com lijo lijo@gmail.com james james@gmail.com thomas thomas@gmail.com';
while ( $dd =~ /\s+(\S*\@\S*.com)\s+/g )
{
print $1."\n";
}


output:

G:\perl_programs>perl perl_occurence.pl
hellokarthik@gmail.com
lijo@gmail.com
james@gmail.com

G:\perl_programs>

Analysis:
Here i have used the pattern /\s+(\S*\@\S*.com)\s+/g  to extract all email-ids from the scalar $dd.Here g signifies globally match all the email -ids from the string. 

No comments:

Post a Comment