Questions And Answers

More Tutorials

Perl Matching and substitution



The “match” operator searches a string for a pattern match. The preceding “m” is usually omitted. The trailing modifiers are as follows
Modifier Meaning
g Match globally; find all occurrences
i Do case-insensitive matching
m Treat string as multiple lines
o Only compile pattern once
s Treat string as a single line
x Use extended regular expressions


Searches a string for a pattern, and replaces any match with replacement. The trailing modifiers are all the same as for the match operator, with the exception of “e”, which evaluates the right-hand side as an expression. The substitution operator works on the default variable ($_), unless the =~ operator changes the target to another variable.


This operator scans a string and, character by character, replaces any characters matching pattern1 with those from pattern2.
Trailing modifiers are:
Modifier Meaning
c Complement pattern1
d Delete found but unreplaced characters
s Squash duplicated replaced characters

This can be used to force letters to all uppercase:

@fields = split(pattern,$input);

Split looks for occurrences of a regular expression and breaks the input string at those points. Without any arguments, split breaks on the whitespace in $_:

@words = split;
is equivalent to
@words = split(/\s+/,$_);

$output = join($delimiter,@inlist);

Join, the complement of split, takes a list of values and glues them together with the provided delimiting string.


In this page (written and validated by ) you learned about Perl Matching and substitution . What's Next? If you are interested in completing Perl tutorial, your next topic will be learning about: Perl Subroutines and Functions.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.