Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regexp question...

Reply
Thread Tools

Regexp question...

 
 
adamomitcheney@kiwis.co.uk
Guest
Posts: n/a
 
      06-13-2005
Hi All,

Apologies in advance if this is a dumb question ("There are no stupid
questions, but there are loads of inquisitive idiots")...

I wanted a regular expression to seperate a filename from a full path
(UNIX). I came up with the following:

$target =~ m%(.+/)(.+)%;
$targetpath = $1;
$targetfile = $2;

Actually, I expected that not to work; but it does, apparently
perfectly. I thought it would fail because I thought the $1 portion of
the regexp would process $target until it found the leftmost '/' and
then stop, leaving everything else to the $2 part. Why am I wrong? Am I
just feeding it a particularly restrictive set of cases?

What regular expression could I use if I wanted to split a UNIX path
into the bottom level (that is, closest to root) directory and the rest
of the path?

Thanks for your indulgence.

Adam...

 
Reply With Quote
 
 
 
 
Fabian Pilkowski
Guest
Posts: n/a
 
      06-13-2005
* http://www.velocityreviews.com/forums/(E-Mail Removed) schrieb:
>
> I wanted a regular expression to seperate a filename from a full path
> (UNIX). I came up with the following:
>
> $target =~ m%(.+/)(.+)%;
> $targetpath = $1;
> $targetfile = $2;
>
> Actually, I expected that not to work; but it does, apparently
> perfectly. I thought it would fail because I thought the $1 portion of
> the regexp would process $target until it found the leftmost '/' and
> then stop, leaving everything else to the $2 part. Why am I wrong?


In regular expressions ".+" is greedy. Hence the first part process
$target until the *rightmost* slash is found.

>
> What regular expression could I use if I wanted to split a UNIX path
> into the bottom level (that is, closest to root) directory and the rest
> of the path?


Avoid that greediness. Please read `perldoc perlre` for details.

$target =~ m%(.+?/)(.+)%;

regards,
fabian
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      06-13-2005
(E-Mail Removed) wrote in news:(E-Mail Removed) oups.com:

> I wanted a regular expression to seperate a filename from a full path
> (UNIX).
> I came up with the following:
>
> $target =~ m%(.+/)(.+)%;
> $targetpath = $1;
> $targetfile = $2;


You do not need the full regex machinery to do accomplish such a
task. In addition, using File::Basename is more appropriate than
rolling your own in this case. See

<URL: http://groups-beta.google.com/group/comp.lang.perl.misc/msg/e8d8519b712c483b?dmode=source>

<URL: http://groups-beta.google.com/groups?q=unur+basename&scoring=d>

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new RegExp().test() or just RegExp().test() Matěj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments