Like
artist says, to handle wierdies like ?, you should use the \Q and \E features of the regular expression system.
Also, keep in mind that with double quotes, you are not really escaping your question marks at all. To see this, try printing your $matchtext. It's not what you intended! Compare the differences between the following two things:
$matchtext1 = "http://www.new.site/somenewpage.pl\?";
print "$matchtext1\n";
$matchtext2 = 'http://www.new.site/somenewpage.pl\?';
print "$matchtext2\n";
Single quotes preserve your backslash, double quotes
use it, putting, as you asked, a
literal quotation mark in your string. This is different from single quotes which put in a backslash then a question mark when presented with the same string.
Maybe that explains some of the mystery. For the rest, rememmber what ? does within a regex. You are in effect saying that the 'l' in "somepage.pl" is optional. Then you replace this text with something with an additional question mark. So, you get this:
s/somenewpage.pl??/somenewpage.pl?/
So, "somenewpage.p" turns into "somenewpage.pl?" and you
still have the "l?" leftover. Hence, "somenewpage.pl?l?".
Why does it do this? With a single question mark, it would be
greedy and grab the 'l', since there is one there. The second question mark tells it to be
conservative and use the shortest possible match, which in this case is the "0" part of "0 or 1 instances of" meaning of "?".
End result? Try this:
$matchtext = "http://www.foo.bar/somepage.pl?";
$substitutetext = "http://www.new.site/somenewpage?";
s/\Q$matchtext\E/$substitutetext/;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.