[Image of Linux]
Linux's Witness Ministry
The Personal Web Pages of Chris X. Edwards

ex-pattern.pl

--------------------------
#!/usr/bin/perl
#ex-pattern.pl

$DNA=">dnaTTTAAGCGCGCGCTTTAAAANNNNNNNNNNNNAATTAATTGCT";
sub show; 
sub hr;

print "original string", $DNA;
print "simple match\n";
$c=($DNA=~m/TA/);
print "\$DNA=~m/TA/\n";
print "pos =", pos($DNA), "\n";
show;

$c=0;
hr;
print "Global match to a scalar in loop:\n";
while ($DNA=~m:T[AG]:g) {
$c++;
print "while loop #",$c,"  \$DNA=~m:T[AG]:g\n";
print "pos =", pos($DNA), "\n";
show;
}

hr;
print "Global match to an array:\n";
@c=($DNA=~m:T[AG]:g);
print "\$DNA=~m:T[AG]:g\n";
print join ("  ", @c), "\n";
print "pos =", pos($DNA), "\n";
show;

hr;
print "varient forms of matching\n";
$c=($DNA=~/TA/g);
print "\$DNA=~/TA/g\n";
print "pos =", pos($DNA), "\n";
show;

$c=($DNA=~m/ta/i);
print "\$DNA=~m/ta/i\n";
show;

hr;
$c=($DNA=~s/^>dna//);
print "simple substitution\n";
print "\$DNA=~s/^>dna//\n";
show;

hr;
$c=($DNA=~s#AA#Aa#g);
print "global substitution\n";
print "\$DNA=~s#AA#Aa#g\n";
show;

$c=($DNA=~s/A/a/g);
print "\$DNA=~s/A/a/g\n";
show;

hr;
$c=($DNA=~s/TA/TA/ig);
print "case insensitive search global substitution\n";
print "\$DNA=~s/TA/TA/ig\n";
show;

hr;
print "replacement string as perl expression\n";
$c=($DNA=~s/TA/rand/eg);
print "\$DNA=~s/TA/rand/eg\n";
show;

hr;
print "fun and games with character translation\n";
$c=($DNA=~tr/ACGTN/acgtn/);
print "character translation\n";
print "\$DNA=~tr/ACGTN/acgtn\n";
show;
$c=($DNA=~y/0-9./AAACCGGTTTN/);
print "\$DNA=~y/0-9./AAACCGGTTTN/\n";
show;

sub show {print "results in: \n", 
          "match returns  ", $c,"\n", 
          "  pre-match->", $`, "  match->", $&, "<-match  ", $', "<-post-match\n", 
          $DNA, "\n\n";}
sub hr {print
"---------------------------------------------------------\n"
;}

--------------------------
Return to Program Examples
This page was created with only free, open-source, publicly licensed software.
This page was designed to be viewed with any browser on any system.
Chris X. Edwards ~ January 2003