Regular Expressions
Regular expressions are used to search for patterns in strings of data.
Pattern-Matching Operators:
Pattern-matching operators are the keywords in Perl that perform pattern matches. The difference between regular expression syntax and pattern-matching operators is that regular expressions allow the programmer to build complex expressions, whereas pattern-matching operators deals with how to use them. The syntax used to perform a pattern match on a string is:
$string =~ /regular expression/expression modifier (optional)
The strings inside / / will be searched for.
The two main pattern matching operators are m//, the match operator, and s///, the substitution operator. There is also a split operator, which takes an ordinary match operator as its first argument but otherwise behaves like a function.
Although we write m// and s/// here, you can pick your own quote characters. On the other hand, for the m// operator only, the m may be omitted if the delimiters you pick are in fact slashes. (You'll often see patterns written this way, for historical reasons.)
Regular Expression Syntax:
There are a lots and lots of regular expressions in Perl. The most common operator used to apply regular expressions on strings is what is called a pattern-binding operator (=~) and (!~). The first compares a string to the pattern and succeeds if the two match. The second binding operator compares the string to the pattern and succeeds if the comparision fails. The syntax.
$string !~ /regular expression/expression modifier (optional)
The rules of regular expression matching
.
Modifiers:
An expression modifier can be added to most regular expressions to modify the behaviour of the expression. The following is an example.
# Create a basic string.
my $string = "Hello World!";
if ($string =~ /"Hello World!"/)
{
print "Case Match!\n";
}
if ($string =~ /"hello WORLD!"/i)
{
print "Case insensitive Match!\n";
}