SWC Header

Cablechip Solutions

web development with Unix, Perl, Javascript, HTML and web services

Title

Cablechip Solution's Blog

Regex : Using a regular expression (perl specific).

There are 3 ways to call a regex:
  1. Use: $x =~ //
  2. Use: $x =~ m//
    or m## or m"" or m{} or anything else you care to choose
  3. Use: qr()
    or qr## or qr"" or qr{}
Why so many ways. First, that's Perl. But it helps with escaping, see examples below. Just choose the delimiters that don't appear inside the rege.

The usual choices are // () ## and ""


$x =~ /andrew/;
$x =~ m/andrew/;
$x =~ m#andrew#;
$x =~ qr(andrew);

#matches speachmarks
$x =~ m/"andrew"/;

#matches HTML tags - there's no need to escape the / in </b>
$x =~ qr# <b>andrew</b> #;

No comments: