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

Searches a string for a pattern match. Modifiers are :

Modifier

Meaning

g

Match globally, i.e., find all occurrences.

i

Do case-insensitive pattern matching.

m

Treat string as multiple lines.

o

Compile pattern only once.

s

Treat string as single line.

x

Use extended regular expressions.

If / is the delimiter, then the initial m is optional. With m, you can use any pair of non-alphanumeric, non-whitespace characters as delimiters.

? pattern ?

This operator is just like the m/ pattern / search, except it matches only once.

qr/ pattern /imosx

Creates a precompiled regular expression from pattern, which can be passed around in variables and interpolated into other regular expressions. The modifiers are the same as those for m// above.

s/ pattern / replacement /egimosx

Searches a string for pattern and replaces any match with the replacement text. Returns the number of substitutions made, which can be more than one with the /g modifier. Otherwise, it returns false (0). If no string is specified via the =~ or !~ operator, the $_ variable is searched and modified. Modifiers are:

Modifier

Meaning

e

Evaluate the right side as an expression.

g

Replace globally, i.e., all occurrences.

cg

Continue search after g failed. No longer supported for s/// as of Perl 5.8.

i

Do case-insensitive pattern matching.

m

Treat string as multiple lines.

o

Compile pattern only once.

s

Treat string as single line.

x

Use extended regular expressions.

Any non-alphanumeric, non-whitespace delimiter may replace the slashes. If single quotes are used, no interpretation is done on the replacement string (the /e modifier overrides this, however).

tr/ pattern1 / pattern2 /cds
y/ pattern1 / pattern2 /cds

This operator scans a string character by character and replaces all occurrences of the characters found in pattern1 with the corresponding character in pattern2. It returns the number of characters replaced or deleted. If no string is specified via the =~ or !~ operator, the $_ string is translated. Modifiers are:

Modifier

Meaning

c

Complement pattern1.

d

Delete found but unreplaced characters.

s

Squash duplicate replaced characters.

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

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