Perl Subsituations





So far in this chapter, you have learned only how to search in strings for text using regular expressions.
But so far all you could do with skill is to then find out whether a particular string matches a regular expression. You may desire to change a string.
The s /// operator acts much like the m// operator except that you specify what you want to replace the found string with. You put the replacement between the second and third slashes.
The s/// operator returns the number of times it substituted. It takes the same modifiers as the m// operator.
Suppose that you want to change all the lowercase "e" characters to uppercase " E ":
$Count = ($Test =~s/e/E/g);
A slightly more complex example would be change all the vowels to the letter V. :
$Count = (Test =- s/[aeiouAEIOU]/V/g) ;
Like the m// operator, if you don't use the =- operator before the s/// operator, Perl assumes that it is working on the special variable $-. Thus, you may use something like the following :
while (){
s^t/;/g, # Change the tabs to vertical bars
...
}
Domain Name Search
|