Regular Expressions
Regular expressions are used several ways in Perl. They’re used in conditionals to determine whether a string matches a particular pattern. They’re also used to find patterns in strings and replace the match with something else.
The ordinary pattern match operator looks like /
pattern
/.
It matches against the $_ variable by default. If the pattern is found
in the string, the operator returns true ("1"); if there is no
match, a false value ("") is returned.
The substitution operator looks like s/
pattern
/
replace
/.
This operator searches $_ by default. If it finds the specified pattern,
it is replaced with the string in replace. If pattern is not
matched, nothing happens.
You may specify a variable other
than $_ with the =~ binding operator (or the negated !~
binding operator, which returns true if the pattern is not matched).
For example:
$text =~ /sampo/;
Pattern-Matching Operators
The following list defines Perl’s pattern-matching operators. Some of the operators have alternative “quoting” schemes and have a set of modifiers that can be placed directly after the operators to affect the match operation in some way.
-
m/pattern/gimosx Searches a string for a pattern match. Modifiers are:
Modifier Meaning gMatch globally, i.e., find all occurrences.
iDo case-insensitive pattern matching. mTreat string as multiple lines. oOnly compile pattern once.
sTreat string as single line. xUse extended regular expressions. If
/is the delimiter, then the ...