Thursday, October 1, 2015

Regular expressions in Perl Scripting

Examples of Regular expressions in Perl Scripting.

#Searching regular expressions using perl script

$SearchStr = "Ram and Laxman were brothers in Ramayana";

if ($SearchStr =~ /Laxman/){

   print "Searched regular expression matched\n";

}else{

   print "Search Keyword do not match\n";

} 



#Searching regular expressions using perl script

$bar = "Ram and Shyam are honest boys";

if ($bar =~ /honest/){

   print "Second time is matching\n";

}else{  

   print "Second time is not matching\n";

}

#Searching regular expressions using perl script

$string = "The chicken is in the white utensils";

$string =~ m/chick/;

print "Before: $`\n";

print "Matched: $&\n";

print "After: $'\n";



#Substitute an expression to another expression in perl script

$expression = "The cat sat on the mat";

$expression =~ s/cat/dog/;

print "$expression\n";



#translate an expression to another expression in perl script

$expression = "The cat sat on the mat";

$expression =~ tr/a/i/;

print "$expression\n";



#translate characters to any other character using perl script

$string = 'foooooooooooooooooooooooooood';

$string =~ tr/o+/o/s; 

print "$string\n";



#translate characters to any other character using perl script

$upstr = "the cat sat on the mat";

$upstr =~ y/a-z/A-Z/;

print "$upstr\n";

Regular Expressions in Perl
Regular Expressions in Perl

No comments:

Post a Comment