When you get the hang of pattern matching, you'll find that substitutions are quite straightforward and very powerful. The substitution operator is s///, which resembles the match operator but has three rather than two slashes. Just as you can do with the match operator, you can substitute any other character for the forward slashes, and you can use the optional i, g, and o switches.
The pattern to be replaced goes between the first and second delimiters, and the replacement pattern goes between the second and third delimiters. This simple example changes $house from henhouse to doghouse:
$house = "henhouse";$house =~ s/hen/dog/; Notice that it isn't possible to use the =~ operator with a literal string as you can when matching, because you can't modify a literal constant. Instead, store the string in a variable and modify that variable.
Here is the brief table of all the operators used for Regular Expressions.
0 comments:
Write a Comment!