For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > perl regexp error , I cant understand what is wrong









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author perl regexp error , I cant understand what is wrong
Meir Yanovich

2006-11-02, 7:56 am



=20
=20

perl regexp error , I cant understand what is wrong=20

Hello all=20
I have simple perl regexp that is searching for pattern in string and
replace it with the same string + addition string=20
here is what I have :

Code:

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


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

and the handle_path function lookes like this :=20


Code:
sub handle_path {

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

}

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

2006-11-02, 7:56 am

"Meir Yanovich" schreef:

> [^+\s*]


That is a character class, not containing "+", whitespace and "*".
A character class is matching a single character.
Is that what you meant by it?

It will match the comma that you have in front of the opening dquotes,
so that gets removed.

--
Affijn, Ruud

"Gewoon is een tijger."

Mumia W.

2006-11-02, 9:57 pm

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' %>";
}
}


Rob Dixon

2006-11-02, 9:57 pm

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 also cannot see what you intend by [^+\s*] in your regex, but it is
unnecessary to achieve what you want with the string you give. You can also make
your code more legible by removing unnecessary escapes. None of the backslashes
you have entered are necessary apart from before the slash character in the
regex, and this can be avoided by changing the regex delimiter, like this:


$rec =~ s#("/.*?(jsp|gif|css|bmp|js)")#handle_path($1,1)#gse;

sub handle_path {

my ($s, $type) = @_;

if ($type == 0) {
return "Env.getPath()+$s";
}
elsif ($type == 1){
return "<%= Env.getPath() %>+$s";
}
}


HTH,

Rob

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com