=~
] [ 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. |
| The first parenthesized subexpression that matched, |
| 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:
| (with |
| matches as many times as possible. |
| searches in a case-insensitive manner. |
| interpolates variables only once. |
treats the string as multiple lines. | |
| treats the string as a single line. |
| allows 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, unless c
is 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/
newtext/
[ e
] [ g
] [ i
] [ m
] [ o
] [ s
] [ x
]Searches the string var (default $_
) for a pattern, and if found, replaces that part with the replacement text.
If successful, sets the special variables as described with m//
and returns the number of substitutions made. Otherwise, it returns false.
Optional modifiers are:
| replaces all occurrences of the pattern. |
| evaluates newtext as a Perl expression. |
For the other modifiers, see m/
pattern/
matching on the page before.
If pattern is empty, the most recent pattern from a previous successful m//
or s///
is used.
See generic Chapter 6.
$
var =~
] tr/
search/
replacement/
[ c
] [ d
] [ s
]Transliterates all occurrences of the characters found in the search list into the corresponding character in the replacement list. It returns the number of characters replaced.
Optional modifiers are:
| complements the search list. |
| deletes all characters found in the search list that do not have a corresponding character in the replacement list. |
| squeezes all sequences of characters that are translated into the same target character into one occurrence of this character. |
See generic Chapter 6.
$
var =~
] y/
search/
replacement/
modifiersIdentical to tr.
If the righthand side of the =~
or !~
is an expression rather than a search pattern, substitution, or transliteration, and its value is not the result of a qr operator, it is interpreted as a string and compiled into a search pattern at runtime.
Returns the position where the last /g
search in scalar left off. Alters the location of G
if assigned to.
Studies the scalar in anticipation of performing many pattern matches on its contents before the variable is next modified.