How handle special chars during regexp 
Author Message
 How handle special chars during regexp

Hi,

I have the following that I can't seem to get to work :

$FILEPATH='/programs/lopez?sub=1996/LOPEZ.1996.09.01.htm';

while ( $FILEPATH =~ /^\/(.*?)\// )
{
  $new_filepath  =  $1;
  $site_dir .= "/$new_filepath";
  $FILEPATH =~ s/^\/${new_filepath}//;  #<- This is the problem line

Quote:
}

print "Site Dir: $site_dir\nExtension is $FILEPATH\n";

It seems the $FILEPATH parses the '/programs' out fine, but when it
gets to lopez?sub=1996 it loops on that FOREVER!  There has got to be
a way to do it and I'm just not seeing it!

Reply via news and email, as my news server isn't the most reliable
these days.

Thanks, Scott



Sun, 21 Feb 1999 03:00:00 GMT  
 How handle special chars during regexp



Quote:
>Hi,

>I have the following that I can't seem to get to work :

>$FILEPATH='/programs/lopez?sub=1996/LOPEZ.1996.09.01.htm';

>while ( $FILEPATH =~ /^\/(.*?)\// )
>{
>  $new_filepath  =  $1;
>  $site_dir .= "/$new_filepath";
>  $FILEPATH =~ s/^\/${new_filepath}//;  #<- This is the problem line
>}

>print "Site Dir: $site_dir\nExtension is $FILEPATH\n";

>It seems the $FILEPATH parses the '/programs' out fine, but when it
>gets to lopez?sub=1996 it loops on that FOREVER!  There has got to be
>a way to do it and I'm just not seeing it!

You need to escape the metacharacters, maybe

  $FILEPATH =~ s|^/\Q$new_filepath||;

will work (well, it does, 'cos I just checked it out :-) or you can
collapse the thing into something like

#!/usr/local/bin/perl -w

$FILEPATH='/programs/lopez?sub=1996/LOPEZ.1996.09.01.htm';
$site_dir = '';
$site_dir .= "/$1" while $FILEPATH =~ s|^/(.*?)(?=/)||;

print "site_dir = $site_dir, FILEPATH = $FILEPATH\n";

Hope this helps,

Mike

--

http://www.stok.co.uk/~mike/       |   PGP fingerprint FE 56 4D 7D 42 1A 4A 9C
http://www.token.net/~mike/        |                   65 F3 3F 1D 27 22 B7 41



Mon, 22 Feb 1999 03:00:00 GMT  
 How handle special chars during regexp

Quote:

> Hi,

> I have the following that I can't seem to get to work :

> $FILEPATH='/programs/lopez?sub=1996/LOPEZ.1996.09.01.htm';

> while ( $FILEPATH =~ /^\/(.*?)\// )
> {
>   $new_filepath  =  $1;
>   $site_dir .= "/$new_filepath";
>   $FILEPATH =~ s/^\/${new_filepath}//;  #<- This is the problem line
> }

> print "Site Dir: $site_dir\nExtension is $FILEPATH\n";

> It seems the $FILEPATH parses the '/programs' out fine, but when it
> gets to lopez?sub=1996 it loops on that FOREVER!  There has got to be
> a way to do it and I'm just not seeing it!

> Reply via news and email, as my news server isn't the most reliable
> these days.

> Thanks, Scott

There are many variations to doing this using REs, but
it is more efficient to make C code do the work (i.e.
avoid loops)...

$FILEPATH='/programs/lopez?sub=1996/LOPEZ.1996.09.01.htm';

sub split_path_file {



Quote:
}

($site_dir, $FILEPATH) = &split_path_file($FILEPATH);

print "Site Dir: $site_dir\nExtension is $FILEPATH\n";



Mon, 22 Feb 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Replacing special chars with special chars

2. cgi handling of accents and special chars

3. Shell special chars in file names?

4. pattern matching with special char

5. escape special chars before loading into MySQL

6. Special chars

7. special chars

8. about special chars when split()

9. Text::CSV with Special Chars?

10. perl/sendmail: encoding special chars

11. is there a way to escape special chars

12. Newbie - Escaping special chars in forms

 

 
Powered by phpBB® Forum Software