7.1. Syntax

The syntax for checking if a string contains the regular expression is $string =~ /$regexp/. For instance the following program checks if a given string contains the word hello (in all lowercase):

use strict;
use warnings;

my $string = shift(@ARGV);

if ($string =~ /hello/)
{
    print "The string contains the word \"hello\".\n";
}
else
{
    print "The string does not contain the word \"hello\".\n";
}

Note that alphanumeric characters in regular expressions stand for themselves, and that behaviour is guaranteed not to change in further versions of perl. Other characters may need to be prefixed with a backslash (\) if placed inside a regular expression.


Written by Shlomi Fish