Regular Expressions

Variable interpolation

You may write variables inside a regular expression.

Example

  $name = "Danny";
  if ($text =~ /$name is here/) {  }

# same as:
# if ($text =~ /Danny is here/) {  }

Meta-characters

As we have seen before, the following characters have special meaning inside regular expressions. If you want to use them literally, add a backslash before them.

\ | ( ) [ { ^ $ * + ? .

Example

To match "5*(12+7)=45. right?" write:
$text =~ /5\*\(12\+7\)=45\. right\?/;

Note:

Closing HTML tags require a backlash inside regular expressions:
$html =~ /<H1>.*<\/H1>/;


Table of Contents.
Previous.