简体   繁体   中英

a simple string substitution does not work

Below is my codes:

my $string1 = '<td><a href="http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3">abcdefg</a><br />(123456)</td>';
my $string2 = 'http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3';


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

And the actual output:

Before string substitution:
<td><a href="http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3">abcdefg</a><br />(123456)</td>
After string substitution:
<td><a href="http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3">abcdefg</a><br />(123456)</td> 

What I expect:

Before string substitution:
<td><a href="http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3">abcdefg</a><br />(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.

That problem can be fixed by adding two characters to your script. What you need is to escape meta characters in $string2 :

$string1 =~ s/\Q$string2//;

The character that causes the match to fail is the question mark ? , which unescaped here ...aspx?... means "match 0 or 1 of the character 'x'". The characters . are wildcards that match anything except newline, which may cause false positive matches. The slashes / , while being meta characters due to being the delimiter of the substitution operator s/// , do not need to be escaped since they are embedded in a string.

Escaping meta characters is most easily done with the \\Q ... \\E escape sequence, inside a regex, or with quotemeta .

It is not a good idea to try and escape these kinds of strings manually, especially if literal matches are all that is required.

Since you are putting in characters that are considered special characters by perl regex, you must escape them out like this:

my $string2 = 'http:\/\/www\.aaa\.com\/downloads\/details\.aspx\?FamilyID=a1b2c3';

Then the expected output will show up when you run your program:

<td><a href="http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3">abcdefg</a><br />(123456)</td>
After string substitution:
<td><a href="">abcdefg</a><br />(123456)</td>

To escape these characters from your string, it is best to just use perl's quotemeta function:

my $string2 = quotemeta('http://www.aaa.com/downloads/details.aspx?FamilyID=a1b2c3');

This will escape the special characters for you and then your regex replace will work fine.

Since you're having issues because of non-escaped regex characters, this solution might be simpler since it does not require you to escape any characters:

substr($string1, index($string1,$string2), length($string2)) = '';

This is based off of this example:

my $name = 'fred';
substr($name, 4) = 'dy'; # $name is now 'freddy'

found in the perldocs for substr .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM