Perl RegEx examples:
#!/usr/bin/perl -w
#
# Playing with RegEx
#
#
#Use grouping to put colons around capital letters
$Test = “Lord Whopper of Fibbing”;
$Test =~ s/([A-Z])/:$1:/g;
print “$Test\n”;
#Straight Forward Substistution
$Test = “Lord Whopper of Fibbing”;
$Test =~ s/L/l/;
print “$Test\n”;
#Use grouping to swap the first and last letters
$Test = “Lord Whopper of Fibbing”;
$Test =~ s/^(.)(.*)(.)$/$3$2$1/;
print “$Test\n”;
#checking values using regex
$sentence = “Best winkles in Sunderland”;
if ($sentence =~ /under/)
{
print “We’re talking about rugby\n”;
}
#checking values using regex ignoring case
$sentence = “Best winkles in Sunderland”;
if ($sentence =~ /Under/i)
{
print “We’re talking about rugby\n”;
print $sentence, “\n”;
}
#checking values using regex ignoring case
# This one has an extra m
$sentence = “Best winkles in Sunderland”;
if ($sentence =~ m/Under/i)
{
print “We’re talking about rugby\n”;
print $sentence, “\n”;
}
#
# Using split
#
$CDV=”Apple,Orange,Banana,Grapes,Pear”;
@fruits=split(/,/,$CDV);
foreach $fruit ( @fruits ){
print $fruit,”\n”;
}