On 11/02/2006 07:11 AM, Meir Yanovich wrote:

perl regexp error , I cant understand what is wrong Hello all I have simple perl regexp that is searching for pattern in string and replace it with the same string + addition string here is what I have :

Code:

my $rec =  q| new Array("Attributes Management"
,"/ResourceManagement/Attribute/attributeFrameset.jsp","/Images/icons/at
tributes.gif",null,"AttributesManagement"),|;


Code:
rec =~ s/[^+\s*](\"\/.*?(jsp|gif|css|bmp|js)\")/handle_path($1,1)/gse;

and the handle_path function lookes like this :

Code:
sub handle_path {

        my $s = $_[0];
        my $type = $_[1];
if($type == 0){
                return "Env.getPath()+".$s;
        }elsif($type == 1){
                my $tmpStr = "\<\%= Env.getPath() \%\>\+".$s;
                return $tmpStr;
                
        }

}

but the result im getting is almost fine .. there is missing comma in
there , and i have no idea why the comma is missing after the switching . for example between the
"Attributes Management" and <%= Env.getPath() %>
Here is the result: new Array("Attributes Management" <%= Env.getPath()
%>+"/ResourceManagement/Attribute/attributeFrameset.jsp"<%=
Env.getPath()
%>+"/Images/icons/attributes.gif",null,"AttributesManagement"),
can someone please tell me what im doing wrong here?


I think your substitution is grabbing too much. I only have a vague idea of what you're trying to do, but this is how I might do it:

use strict;
use warnings;

my $rec =  q| new Array("Attributes Management"
,"/ResourceManagement/Attribute/attributeFrameset.jsp",
"/Images/icons/attributes.gif"
,null,"AttributesManagement"),|;

$rec =~ s/"([^"]+\.[[:alpha:]]{2,3})"/
    '"' . handle_path($1,1) . '"'/eg;
print $rec, "\n";

sub handle_path {
    my ($s, $type) = @_;
    if (0 == $type) {
        "Env.getPath()+'$s'";
    } else {
        "<% Env.getPath() + '$s' %>";
    }
}



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to