Skip to Content
Learning Perl, Fourth Edition
book

Learning Perl, Fourth Edition

by Randal L. Schwartz, Tom Phoenix, brian d foy
July 2005
Beginner
312 pages
9h 23m
English
O'Reilly Media, Inc.
Content preview from Learning Perl, Fourth Edition

Chapter 9. Processing Text with Regular Expressions

You can use regular expresssions to change text, too. So far, we’ve only shown you how to match a pattern. Now, we’ll show you how to use patterns to locate the parts of strings that you want to change.

Substitutions with s///

If you think of the m// pattern match as being like your word processor’s “search” feature, the “search and replace” feature would be Perl’s s/// substitution operator. This replaces whichever part of a variable[214] matches a pattern with a replacement string:

    $_ = "He's out bowling with Barney tonight.";
    s/Barney/Fred/;  # Replace Barney with Fred
    print "$_\n";

If the match fails, nothing happens, and the variable is untouched:

    # Continuing from above; $_ has "He's out bowling with Fred tonight."
    s/Wilma/Betty/;  # Replace Wilma with Betty (fails)

The pattern and the replacement string could be more complex. Here, the replacement string uses the first memory variable, $1, which is set by the pattern match:

    s/with (\w+)/against $1's team/;
    print "$_\n";  # says "He's out bowling against Fred's team tonight."

Here are some other possible substitutions. (These are here only as samples; in the real world, it would not be typical to do so many unrelated substitutions in a row.)

 $_ = "green scaly dinosaur"; s/(\w+) (\w+)/$2, $1/; # Now it's "scaly, green dinosaur" s/^/huge, /; # Now it's "huge, scaly, green dinosaur" s/,.*een//; # Empty replacement: Now it's "huge dinosaur" s/green/red/; # Failed match: still "huge dinosaur" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Perl, 3rd Edition

Learning Perl, 3rd Edition

Tom Phoenix, Randal L. Schwartz
Learning Perl, 6th Edition

Learning Perl, 6th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Learning Perl, 7th Edition

Learning Perl, 7th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
Programming Perl, 3rd Edition

Programming Perl, 3rd Edition

Larry Wall, Tom Christiansen, Jon Orwant

Publisher Resources

ISBN: 0596101058Catalog PageErrata