shadowline
Matching - Extras << Search & Replace >> Files - Part 1
SEARCH & REPLACE.
Just as the title implies, this is to look for certain patterns and replace them with a different specified string.



Substitution is specified with s. It is placed before the first forward slash surrounding the search expression. The string that will be used to replace the matching pattern is included within a second set of slashes.

$string =~ s/apples/oranges/;

In the above example, the expression being searched for in the variable $string is "apples". Where ever this is found, it will be replaced by "oranges".

$string1 = "I love to eat apples all day. I can eat apples every day! Apples are yummy!";
$string1 =~ s/apples/oranges/;
print "The resulting value is : $string1 \n";

See the example

The above will react to the first found result. To make it search/replace through the entire string, use the g option after the last forward slash.

$string1 = "I love to eat apples all day. I can eat apples every day! Apples are yummy!";
$string1 =~ s/apples/oranges/g;
print "The resulting value is : $string1 \n";

See the example

Keep in mind that Perl is CaSe SeNsItIvE. To get rid of the case sensitivity, use the i option after the "g".

$string1 = "I love to eat apples all day. I can eat apples every day! Apples are yummy!";
$string1 =~ s/apples/oranges/gi;
print "The resulting value is : $string1 \n";

See the example

The g and i option have been discussed on the previous webpage. There is one more option which is e which stands for expression. This will parse an expression to its value during the replacement action.

$string =~ s/apples/3*3/g;

The above example will replace any occurances of "apples" with the text "3*3". The value of 3*3 was not produced during the replace.

$string =~ s/apples/3*3/ge;

The above example will replace any occurances of "apples" with "9". The value of 3*3 was parsed with the expression option turned added.



The tr option is for translation. It is very similar to the substitution option except it replaces on a "per character" basis instead of a full word basis. Also unlike the substitution option, the translation option searches through an entire string without having to activate the global addition. The translation option is enabled by adding tr before the first forward slash of an expression.

$string1 = "today is the best day of all";
if ($string1 =~ tr/thed/abc/){
print "String1 now contains $string1. \n";
} else {
print "No match found.: \n";
}

See the example

The output should have produced : aocay is abc bcsa cay of all

Remember, this is searching by each character instead of full words.
Every t will be replaced by an a.
Every h will be replaced by a b.
Every e will be replaced by a c.
The character d does not have a corresponding match replacement, so it defaults to the last character possible which is c.

A few options may be used with the tr. First is c which states that everything except for the selected character are to be translated. This option is enabled by adding the c character after the last forward slash of a reg expression.

$string1 =~ tr/0-9/\t/c;

The above example will replace any character that is not a digit between 0 and 9 with a tab space.

The insensitive i option is not used with tr. To specify the full range of letters, use "[a-z][A-Z]".

The next avialable option is d which is used for delete. It goes after the trailing forward slash as well. It delete all the specified characters in a string.

$string1 = "today is the best day of all";
if ($string1 =~ tr/a//d){
print "String1 now contains $string1. \n";
} else {
print "No match found.: \n";
}

See the example

The output should have produced : tody is the best dy of ll

Did you notice there was no replacement string specified? This is because all of the letters found will be deleted, not replaced.

The last option is s which stands for squeeze. This has only one replacement character for every group of matching characters to be replaced.

Here is an example of a regular script without the s.

$string1="There are 3 options associated with the translation option";
$string1 =~ tr/a-z/9/;
print "$string1";

See the example

In the above example, each lowercase letter was replaced by a 9. Now here is the example again with the s added in.

$string1="There are 3 options associated with the translation option";
$string1 =~ tr/a-z/9/s;
print "$string1";

See the example

Instead of replacing every letter encountered with the number 9, only the strings of matching characters are replaced. This option can also be used in conjunction with the c option to perform the inverse of the example we just saw.
Matching - Extras << Search & Replace >> Files - Part 1
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow