Home: Perl Programming Help: Regular Expressions:
Forward slash to backward slash and strip off y:



triveni1125
New User

Dec 23, 2009, 5:22 PM


Views: 9710
Forward slash to backward slash and strip off y:

can someone please help me in writing the regular expression for the following..
actually fileVapth we have "Y:\default\main\safari\Airline\WORKAREA\Workarea01/html/jetnet/china.html";

But I need to change it to /default/main/safari/Airline/WORKAREA/Workarea01/html/jetnet/china.html ---All fwd slashes and stripoff y:
For this i tried to write the regex as following...But did not workout can u please help...

1.I tried to replace backwd slashes with fwd slashes using $fileVpath=~ s# \ # / #ig;
2.To strip off y:$fileVpath=~ s# ^y:(.*) # $1 #i;

Following is the program...
#!c:/perl/bin
my $fileVpath="Y:\default\main\safari\Airline\WORKAREA\Workarea01/html/jetnet/china.html";
$$fileVpath~ s# \ # / #ig; #to replace backwd slashes with fwd slashes#
print "string3 is now $string3";
$string3=~ s# ^y:(.*) # $1 #i; #To strip off y:the string3 values should be /default/main/safari/Airline/WORKAREA/Workarea01/html/jetnet/china.html#
print "string3 is now $string3";

Thanku,
Triveni


toolic
User

Dec 24, 2009, 10:16 AM


Views: 9692
Re: [triveni1125] Forward slash to backward slash and strip off y:

\ is special, and it needs to be escaped: \\


Code
use strict; 
use warnings;

my $str = 'Y:\default\main\safari\Airline\WORKAREA\Workarea01/html/jetnet/china.html';
$str =~ s/^Y://;
$str =~ s{\\}{/}g;
print "$str\n";



(This post was edited by toolic on Dec 24, 2009, 10:18 AM)


shawnhcorey
Enthusiast


Dec 24, 2009, 11:21 AM


Views: 9688
Re: [triveni1125] Forward slash to backward slash and strip off y:

If you're going to be playing with file paths, I suggest you use File::Spec. It is a standard module, and like all standard modules, it's installed along with Perl.

See `perldoc File::Spec` or http://perldoc.perl.org/File/Spec.html

For a list of standard modules, see `peldoc perlmodlib` or http://perldoc.perl.org/perlmodlib.html

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".