For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > How to take apart the filepath using regular expression?









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 How to take apart the filepath using regular expression?
Marco

2006-01-26, 9:55 pm

I have a Path link with whitespace \Inetpub\wwwroot/Mail Group
How can I use regular expression to take it apart?
I try to use ($host,
$Firstfile)=($pathlink=~m|([/^\Inetpub/]+)(\S*)$|), but seems it failed
because of the whitespace between Mail Group.
Can someone kindly help me to solve this annoying problem?
Thank you so much in advance.

usenet@DavidFilmer.com

2006-01-26, 9:55 pm

Marco wrote:
> How can I use regular expression to take it apart?


Why would you want to?

#!/usr/bin/perl

use File::Basename; # Standard module included in Perl

my $filename = '\Inetpub\wwwroot/Mail Group';

my ($name, $path) = fileparse($filename,@suffixlist);
print "$name\n$path\n";

__END__

--
http://DavidFilmer.com

usenet@DavidFilmer.com

2006-01-26, 9:55 pm

use...@DavidFilmer.com wrote:
> my ($name, $path) = fileparse($filename,@suffixlist);


Oops - leave the suffixlist off since you don't care about that; so
make it:

my ($name, $path) = fileparse($filename);

Xicheng

2006-01-26, 9:55 pm

Marco wrote:
> I have a Path link with whitespace \Inetpub\wwwroot/Mail Group
> How can I use regular expression to take it apart?
> I try to use ($host,
> $Firstfile)=($pathlink=~m|([/^\Inetpub/]+)(\S*)$|), but seems it failed
> because of the whitespace between Mail Group.
> Can someone kindly help me to solve this annoying problem?
> Thank you so much in advance.

If you want to separate "Mail Group" with "\Inetpub\wwwroot", then you
can do:
$_ = q(\Inetpub\wwwroot/Mail Group);
($a,$b) = m{^(.*)/([^/]*)$};
# $a = '\Inetpub\wwwroot' $b = 'Mail Group'

Xicheng

Marco

2006-01-26, 9:55 pm

thank you so much....it did work....really appreciate for that. :)

Marco

2006-01-26, 9:55 pm

thank you .....David & Xicheng..you guys are genius........thanks
again.....

Marco

2006-01-26, 9:55 pm

one more question.....

\Inetpub\wwwroot/Mail Group/Customer Email

Is it possible that separate it like:
(1) Inetpub
(2) wwwroot/Mail Group

and keep take apart it like

(1)wwwroot
(2)Mail Group/Customer Email

I just want to take \Inetpub\wwwroot/ away the keep the rest.....thank
you so much in advance....

Xicheng

2006-01-26, 9:55 pm

Marco wrote:
> one more question.....
> \Inetpub\wwwroot/Mail Group/Customer Email
>
> Is it possible that separate it like:
> (1) Inetpub
> (2) wwwroot/Mail Group

$_=q(\Inetpub\wwwroot/Mail Group/Customer Email);
my ($a,$b)=m{^\\(.*?)\\(.*)/.*$};
print "$a\n$b";

> and keep take apart it like
>
> (1)wwwroot
> (2)Mail Group/Customer Email

$_=q(\Inetpub\wwwroot/Mail Group/Customer Email);
my ($a,$b)=m{^.*\\([^/]*?)/(.*)$};
print "$a\n$b";

> I just want to take \Inetpub\wwwroot/ away the keep the rest.....thank
> you so much in advance....


my ($a)=m{^[^/]*?/(.*)$};
print $a;

you may take a look at something about greedy/non-greedy and regex
character class.
Good luck,
Xicheng

Marco

2006-01-27, 6:57 pm

Hi Xicheng

It worked perfect. thank you so much....really appreciate for your
kindly help. thank you again..

Marco

2006-01-27, 6:57 pm

Hi Xicheng

It worked perfect. thank you so much....really appreciate for your
kindly help. thank you again..

Sponsored Links







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

Copyright 2008 codecomments.com