Regular Expressions

Anchoring a pattern to the beginning or end of a line or string

To force matching of your pattern to the beginning of the string, write a caret ^ as the first character of the regular expression.

To force the matching to the end of the string, write a dollar sign $ as the last character of the regular expression.

Example 1

We have previously learned how to extract desired lines from a Swiss-Prot entry, using the substring function.

Now let us see how to do that using a regular expression matching (though it is less efficient in this case).

To print the "description" line, which starts with DE, we write:

#!/usr/bin/perl

my $sp_file = "sources/sp_entry";

open (SP, $sp_file) || die "cannot open sp_entry": $!";

while (my $line = <SP>) {
  if ($line =~ /^DE/) {
     print $line;
  }
}
Result:
DE   MUSCARINIC ACETYLCHOLINE RECEPTOR M1.
Note: if we omitted the caret from the regular expression:
if ($line =~ /DE/)
We would have got:
DE   MUSCARINIC ACETYLCHOLINE RECEPTOR M1.
RA   ARDEN J.R., NAGATA O., SHOCKLEY M.S., PHILIP M., LAMEH J., SADEE W.;
CC       CELLULAR RESPONSES, INCLUDING INHIBITION OF ADENYLATE CYCLASE,
CC       BREAKDOWN OF PHOSPHOINOSITIDES & MODULATION OF POTASSIUM CHANNELS
     SERSQPGAEG SPETPPGRCC RCCRAPRLLQ AYSWKEEEEE DEGSMESLTS SEGEEPGSEV

Example 2

To match all file names or paths that have the .pl extension, write:
if ($file =~ /\.pl$/) {
  ___;
  ___;
  ___;
}

# Note: there is a backslash before the dot, since we mean a literal
# dot and not "any character except newline" (see here).


Table of Contents.
Previous | Next.