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.

$1

The first parenthesized subexpression that matched, $2 the 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.

g

matches as many times as possible.

i

searches in a case-insensitive manner.

o

interpolates variables only once.

m

treats the string as multiple lines. ^ and $ will match at embedded newline characters.

s

treats the string as a single line. . will match embedded newline characters.

x

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:

g

replaces all occurrences of the pattern.

e

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:

c

complements the search list.

d

deletes all characters found in the search list that do not have a corresponding character in the replacement list.

s

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/modifiers

Identical 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.

pos scalar

Returns the position where the last /g search in scalar left off. Alters the location of G if assigned to.

study scalar

Studies the scalar in anticipation of performing many pattern matches on its contents before the variable is next modified.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset