
Zhris
User
Jun 27, 2010, 12:08 PM
Post #4 of 5
(2450 views)
|
|
Re: [kvn] Substitutions in an html file
[In reply to]
|
Can't Post
|
|
Hey, The $search expression isn't escaped properly (i.e. parts which shouldn't be escaped, are) and (.*) will match after the "closing" single quote '. You also shouldn't have put the $replace expression in a variable because $1 is being interpolated before searching/replacing. I have tested our scripts, made some changes, and here is a working example:
#!/usr/bin/perl use strict; my $search = "\Q<?=\$_['\E([^']*)\Q']?>\E"; while (my $line = <DATA>) { if ($line =~ m/$search/) { print "$line\n"; $line =~ s/$search/<?=NLS($1)?>/g; print "$line\n"; } } __DATA__ <li><a href="#tab3"><em><?=$_['Daily']?> - <?=$_['RPO Graph']?></em></a></li> Output:
<li><a href="#tab3"><em><?=$_['Daily']?> - <?=$_['RPO Graph']?></em></a></li> <li><a href="#tab3"><em><?=NLS(Daily)?> - <?=NLS(RPO Graph)?></em></a></li> Things to note: - I have used \Q and \E to separate groups where I don't want special characters to be activated, and I have escaped the $ in $_ to stop $_ from being interpolated. - I have no longer put "replace" in a variable because I don't want $1 to be interpolated yet. I have kept "search" in a variable otherwise the single quotes create an issue. - Instead of using (.*) I have used ([^']*) to ensure that it only matches anything except a single quote ' (stops any matching after the single quote '). - To ensure all occurrences are searched and replaced, I used the g switch at the end of the regular expression. Chris
(This post was edited by Zhris on Jun 27, 2010, 1:45 PM)
|