Simple Pattern Matches

To test whether a variable matches a string, use the =~ operator:


  $a = 'gatttccaa';
  print "contains three t's" if $a =~ /ttt/;
  print "contains an EcoRI site" if $a =~ /gaattc/

Some Simple Regular Expression Components

Some symbols between the // are special:

^
Matches the beginning of the string.

$
Matches the end of the string.

\w
Matches any single word character (e.g. a-z, A-Z, 0-9).

\w+
Matches one or more word characters.

\d
Matches a single digit.

\d+
Matches one or more digits.

  $a = '367-8380';
  print "This is an OK telephone number.\n" if $a =~ /^\d\d\d-\d\d\d\d$/;

<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Wed Oct 11 19:01:50 EDT 2000