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


--
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