Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a simple string substitution does not work 1

Status
Not open for further replies.

whn

Programmer
Oct 14, 2007
265
US
Below is my codes:
Code:
my $string1 = '<td><a href="[URL unfurl="true"]http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3">abcdefg</a><br[/URL] />(123456)</td>';
my $string2 = '[URL unfurl="true"]http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3';[/URL]


print "Before string substitution:\n$string1\n";
$string1 =~ s/$string2//;
print "After string substitution:\n$string1\n";

And the actual output:
Code:
Before string substitution:
<td><a href="[URL unfurl="true"]http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3">abcdefg</a><br[/URL] />(123456)</td>
After string substitution:
<td><a href="[URL unfurl="true"]http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3">abcdefg</a><br[/URL] />(123456)</td>

What I expect:
Code:
Before string substitution:
<td><a href="[URL unfurl="true"]http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3">abcdefg</a><br[/URL] />(123456)</td>
After string substitution:
<td><a href="">abcdefg</a><br />(123456)</td>

could someone please tell me what is wrong in my code?

Thanks.
 
One way is to escape chars that have special meaning in regexps, or to declare $string2 as a regexp and use the escape character:
Code:
my $string2='http:\/\/[URL unfurl="true"]www\.aaa\.com\/downloads\/details.aspx\?FamilyID=a1b2c3';[/URL]
my $string2=qr|\Q[URL unfurl="true"]http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3|;[/URL]

: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Hi

Code:
[navy]$string1[/navy] [teal]=~[/teal] [b]s[/b][fuchsia]/[highlight #fcc]\Q[/highlight]$string2[highlight #ccf]\E[/highlight]//[/fuchsia][teal];[/teal]

man perlre said:
[tt][highlight #fcc]\Q[/highlight] quote (disable) pattern metacharacters till \E
[highlight #ccf]\E[/highlight] end either case modification or quoted section, think vi[/tt]
( man perlre | Regular Expressions | Escape sequences )

Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top