 |
|
Home:
Perl Programming Help:
Regular Expressions:
Re: [kvn] Substitutions in an html file:
Edit Log
|
|

Zhris
User
Jun 27, 2010, 12:08 PM
Views: 2609
|
|
Re: [kvn] Substitutions in an html file
|
|
|
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)
|
|
|
Edit Log:
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 12:10 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 12:14 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 12:40 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 12:41 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 12:42 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 12:47 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 12:48 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 12:50 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 12:51 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 12:54 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 12:58 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 1:01 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 1:02 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 1:23 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 1:24 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 1:34 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 1:35 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 1:36 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 1:37 PM
|
|
Post edited by Zhris
(User) on Jun 27, 2010, 1:45 PM
|
|
|  |