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 - Perl Tutorial
From the course: Perl 5 Essential Training
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
-
-
-
(Locked)
About regular expressions4m 8s
-
(Locked)
Matching text2m 9s
-
(Locked)
Common modifiers4m 58s
-
(Locked)
Extracting matches2m 5s
-
(Locked)
Getting a list of matches1m 46s
-
(Locked)
Simple matches2m 46s
-
(Locked)
Matching wildcards4m 38s
-
(Locked)
Matching classes of characters4m 23s
-
(Locked)
Matching metacharacters1m 37s
-
(Locked)
Search and replace4m 32s
-
(Locked)
Splitting strings2m 18s
-
(Locked)
-
-
-
-
-
-
-