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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matching and Substituting part of url

Status
Not open for further replies.

forces1

Programmer
Apr 5, 2007
29
NL
Hi all,

In a script of mine I have an url catched in a variable. Then, for custom url's, to let the script run well, I have to remove a part of it. I use the next code for that:
Code:
my $fout1 = "web/show"; # This is the part of the url I want to remove, $ol_info[1] is the url

if($ol_info[1] =~ m/$fout1/){
$ol_info[1] =~ s/[$fout1]//isg;
}

All good and well, but when I run this part, the substitution will remove all of the characters and symbols mentioned in the 'web/show'. But that's not what I want, I only want to remove the part 'web/show' and leave the rest of the url unharmed.

Could you please tell me how to fix this? You've proven to be a great help before.
 
Using brackets creates a character class. You just want to remove a literal string, therefore use \Q and \E to escape your interpolated value in the regex.

Also, doing a existence check before replacement is redundant.

Code:
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$fout1[/blue] = [red]"[/red][purple]web/show[/purple][red]"[/red][red];[/red] [gray][i]# This is the part of the url I want to remove, $ol_info[1] is the url[/i][/gray]

[blue]$ol_info[/blue][red][[/red][fuchsia]1[/fuchsia][red]][/red] =~ [red]s/[/red][purple][purple][b]\Q[/b][/purple][blue]$fout1[/blue][purple][b]\E[/b][/purple][/purple][red]/[/red][purple][/purple][red]/[/red][red]isg[/red][red];[/red]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top