Previous   -   Index   -   Next  >


Regular Expressions


As soon as we start processing form data we need to validate it. The most obvious way to achieve this is to use regular expressions - no discussion of Perl can be complete without mentioning regular expressions anyway (although they really deserve a chapter of their own).

The grammar of regular expressions is more-or-less the same as we met in JavaScript.
(The portability of regular expressions makes them important tools.)

The Matching Operator

There are many ways that a regular expression can use the matching operator m which returns true if the regular expression matches the string, otherwise false. The most common is to drop the m and use / to delimit the regular expression, in which case the matching operator is applied by default. (Almost) any non-alphanumeric and non whitespace character can be used as a delimiter, in which case the m is required.

The regular expression is usually applied to a string using the binding operator =~ although by default the regular expression is applied to the default variable $_. The logic is inverted using the !~ operator. The following are all equivalent:

   $string =~ /pattern/
   $string =~ m/pattern/
   $string =~ m+pattern+
   $string =~ m,pattern,
   $string =~ m]pattern]
   $string =~ m[pattern]
   $string =~ m)pattern)
   $string =~ m(pattern)
   $string =~ m}pattern}
   $string =~ m{pattern}

but not

   $string =~ m(pattern(
   $string =~ m{pattern{
   $string =~ m[pattern[

If you are not sure what is happening... try it on the command line.

   bukowski -->perl -w
   $foo = 'the cat sat on the mat';
   if ($foo =~ m@at@) { print "found\n" }

      <cntrlD>

   found

And using the default variable (omitting the control D for brevity):

   bukowski -->perl -w
   @foo = qw(the CAT would rather sit on the attractive MaT);
   foreach (@foo) {
      if (/at/i) { print "$_\n" }
   }
   CAT
   rather
   attractive
   MaT

This example also illustrates the use of the i modifier.

The Substitution Operator

The substitution operator s works in much the same way but requires two operands, a regular expression and a substitution string.

   bukowski -->perl -w
   $foo = 'the CAT would rather sit on the attractive MaT';
   $bar = ($foo =~ s/at/ow/ig);
   print "$bar substitutions\n";
   print "$foo\n";
   4 substitutions
   the Cow would rowher sit on the owtractive Mow

What would happen if the regular expression were changed to s/at/ow/i?
What would happen if the regular expression were changed to s/.at/ow/g?
Try some regular expressions with this JavaScript form.


Previous   -   Index   -   Next  >

best viewed using Mozilla browsers
© k.mcmanus 2004
Valid XHTML 1.0! . Valid CSS . WCAG priority 3 approved