Search and Replace Functions
- [ expr
=~] [ m ]/pattern/[ modifiers ] Searches expr (default
$_) for a pattern.For
=~, its negation!~may be used, which is true when=~would return false, and vice versa.After a successful match, the following special variables are set:
$&The string that matched.
$`The string preceding what was matched.
$'The string following what was matched.
$1The first parenthesized subexpression that matched,
$2the second, and so on.$+The last subexpression that matched.
@-The start offsets of the match and submatches.
@+The corresponding end offsets.
%+Named subpatterns that matched.
%-All named subpatterns.
If used in list context, a list is returned consisting of the subexpressions matched by the parentheses in pattern, i.e.,
($1,$2,$3, . . . ).Optional modifiers include
adilmopsux,as described in the previous section. Additional modifiers are:c(with
g) prepares for continuation.gmatches as many times as possible.
If pattern is empty, the most recent pattern from a previous successful m
//or s///is used.With
g, the match in scalar context can be used as an iterator. The iterator is reset upon failure, unlesscis also supplied.- [ expr
=~] m?pattern?[ modifiers ] This is just like the
/pattern/search, except that it matches only once between calls to the reset operator.- [
$var=~] s/pattern/newtext/[ modifiers ] Searches the string var (default
$_) for a pattern, and if found, replaces that part with the replacement text.If successful, sets the special variables ...