next up previous
Next: About this document ... Up: perl2nd Previous: Regular expression

Subsections

More pattern matching

Match modifier

Different pattern delimiter

Special match variables

$&  # the part of the string that actually matched the pattern
$`  # the part of the string before the match
$'  # the part of the string after the match

Example:

my $seq = 'TTTGAATTCAAA';
if ($seq =~ /GAATTC/) { # EcoRI site
  print "Found EcoRI site: $`:$&:$'\n";
}

This is similar to $1, $2 etc. when ( ) was used in regex, but it is automatic.

Useful for checking that your complex regex is correct.

pos() and m//g

my $seq="ATGTTTCCCTTTAAA";

while($seq=~/TTT/ig){
  print (pos($seq), ":", length($&), ":", 
	 pos($seq) - length($&) + 1, ":", "$&\n");
}

Perl one-liner

Where to go


next up previous
Next: About this document ... Up: perl2nd Previous: Regular expression
Naoki Takebayashi 2011-10-19