Chapter 23. Search and Replace Functions
- [ expr
=~] [ m ]/pattern/[g[c] ] [i] [m] [o] [s] [x] 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.
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 are:
c(with
g) prepares for continuation.gmatches as many times as possible.
isearches in a case-insensitive manner.
ointerpolates variables only once.
mtreats the string as multiple lines.
^and$will match at embedded newline characters.streats the string as a single line.
.will match embedded newline characters.xallows for whitespace and comments.
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.See generic Chapter 6.
?pattern?This is just like the
/pattern/search, except that it matches only once between calls to the reset operator.- [
$var=~] s/pattern ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access