I have Perl code:
my $s = "The+quick+brown+fox+jumps+over+the+lazy+dog+that+is+my+dog";
I want to replace every +
with space and dog
with cat
.
I have this regular expression:
$s =~ s/\+(.*)dog/ ${1}cat/g;
But, it only matches the first occurrence of +
and last dog
.
You can use the 'e' modifier to execute code in the second part of an s///
expression.
$s =~ s/(\+)|(dog)/$1 ? ' ' : 'cat'/eg;
If $1
is true, that means the \+
matched, so it substitutes a space; otherwise it substitutes "cat".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With