Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing a string containing a question mark

Tags:

regex

perl

This is my code:

$html = 'Is this a question? Maybe.';
$old = 'question?';
$new = 'reply?';

$html =~ s/$old/$new/g;
print $html; exit;

Output is:

Is this a reply?? Maybe.

Desired output:

Is this a reply? Maybe.

What am I doing wrong?

like image 332
P.Henderson Avatar asked Feb 23 '26 11:02

P.Henderson


1 Answers

Use quotemeta to escape the ?:

$html = 'Is this a question? Maybe.';
$old = quotemeta 'question?';
$new = 'reply?';

$html =~ s/$old/$new/g;
print $html; exit;
like image 171
toolic Avatar answered Feb 25 '26 11:02

toolic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!