From the course: Perl 5 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Search and replace

Search and replace - Perl Tutorial

From the course: Perl 5 Essential Training

Start my 1-month free trial

Search and replace

- [Voiceover] Regular expressions are also commonly used to search and replace text in strings. Here's a working copy of replace.pl from Chapter 9 of the exercise files. And this is a simple replacement, we search for the word line and replace it with the word string, and when I run this you'll see it says This is a string of text instead of "This is a line of text". Now if I wanted to replace every occurrence of the letter s or the letter e with the letter x, I would simply do this. And when I build and run this, you notice that it replaces just the first occurrence, not all of them because I need to put in the g here to make this search and replace global. And now, my result is has x's in place of all of the s's and e's, and that extra one there is of course the x that was already in the string that did not need to get replaced. And so you can see that with literal strings, and with classes of strings, this is actually really, really easy. You can also extract and reuse parts of the…

Contents