Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Perl, the substitute operator s
In Perl, the s (or substitute) operator allows you to match a regular expression and replace the part of your incoming string that matched with another string. Your incoming string should be specified to the left of an =~ operator and is changed in situ. For example:

$sample = "The cat sat on the mat";
$sample =~ s/.at/dog/;


Would replace cat (as "." means any character) with dog. It would stop at that point, as the s operator only changes a single occurence unless a g for global modifier is added on the end. So

$sample = "The cat sat on the mat";
$sample =~ s/.at/dog/g;


Would give you The dog dog on the dog

• if you don't specify the variable that s is to work on, it works on the contents of $_

• if you add an e modifier, it performs the output string as a perl expression and substitues in the result.

• if you specify $1 or \1 in the output string, you'll mean the first captured part of the incoming string that matched, and so on.

• if you use the value returned by the substitute operator, you'll find that it contains the number of changes made.

There's an example of all of these features just added to our web site here
(written 2007-06-08 07:02:31)

 
Associated topics are indexed under
P212 - Perl - More on Character Strings

Back to
Bathtubs and pecking birds
Previous and next
or
Horse's mouth home
Forward to
Asda opening large new store in Melksham

Some other Articles
Melksham Art Cafe
Perl - functions for directory handling
Object Relation Mapping (ORM)
Asda opening large new store in Melksham
Perl, the substitute operator s
Bathtubs and pecking birds
for loop - how it works (Perl, PHP, Java, C, etc)
Judging the quality of contributed Perl code
Sunday afternoon
What are factory and singleton classes?
1780 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho