To replace all occurrences of a substring in a string using regular expression in Perl, you can use the s///
operator.
In this example,
$str
.s///
operator to replace all occurrences of "Hello" with "Hi".$str = 'Hello World Hello';
$str =~ s/Hello/Hi/g;
print $str;
Hi World Hi
In this example,
$str
.s///
operator to replace all occurrences of digits (\d) with 'Y'.$str = 'Hello123World789';
$str =~ s/\d/Y/g;
print $str;
HelloYYYWorldYYY
In this tutorial, we learned How to Replace a Substring in a String using Regular Expression in Perl language with well detailed examples.