infyniti has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to modify all the href links on the website to a javascript. I wrote a perl module to search and replace the text. But I couldnt able to end the bracket opened for the javascript. Any ideas on how to close the bracket opened for the javascript.
Thanks in advance
Orginal href text in a page.
<a href="/exitPage/index.jsp?tname=offers&amp;tsrc=ownermag&amp;type=l +ink" onmouseover="rollover('offers','img/offers_on.gif');" onmouseout +="rollover('offers','img/offers_off.gif');"><img src="img/offers_off. +gif" width="139" height="16" alt="" border="0" name="offers"></a>

Modified text :-

<a href="javascript:flexWin('?tname=offers&amp;tsrc=ownermag&amp;type= +link" onmouseover="rollover('offers','img/offers_on.gif');" onmouseou +t="rollover('offers','img/offers_off.gif');"><img src="img/offers_off +.gif" width="139" height="16" alt="" border="0" name="offers"></a>


Perl Module :-

#!/usr/bin/perl #the path to the files $serverpath = "/default/main/test"; #the docs you want changed $doctype = "html"; #the string to look for $find = "/exitPage/index.jsp?"; #what you want to replace it with $replace = "javascript:flexWin('"; #ok, getting started print "Finding \"$find\" and replacing it with \"$replace\"\n"; #going to directory opendir(DIR, "$serverpath"); #looking for files @files = grep(/\.$doctype$/,readdir(DIR)); closedir(DIR); #lining the files up foreach $file(@files){ $pa_fi = "$serverpath/$file"; #opening the file to read it print "opening file $pa_fi\n"; open(FILE,"$pa_fi"); my(@subm) = <FILE>; close(FILE); #backup the document rename $pa_fi, $pa_fi . '.bac'; print "making backup\n"; $n=0; #printing new foreach $line (@subm) { $line =~ s/$find/$replace/g; open(SUB,">>$pa_fi"); print SUB $line; close(SUB); $n++; } print "$n lines searched in $file\n"; }

Replies are listed 'Best First'.
Re: String manipulation
by tlm (Prior) on Apr 04, 2005 at 14:32 UTC

    s,/exitPage/index.jsp?([^"]+),javascript:flexWin($1),g;
    Although it is masochistic to munge HTML with raw regexps; use HTML::TreeBuilder or something like it instead.

    And by all means, please update your post and fix the missing code tags.

    the lowliest monk

Re: String manipulation
by phaylon (Curate) on Apr 04, 2005 at 14:19 UTC
    Please put your code in <code>-tags. You might be interested in searching CPAN for "html link", which gives you some already invented link-extraction wheels.

    hth,p

    Ordinary morality is for ordinary people. -- Aleister Crowley