Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 20th, 2001, 08:48 AM
churcr1 churcr1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2001
Location: United Kingdom
Posts: 9 churcr1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to churcr1
If anyone knows regular expressions, please could you lend a hand.

I am using File::Find to traverse a directory structure, the directory to start traversing from is supplied at the command line.

A typical filepath I get would be : -

d:\nucouk\prestage\products\motor\index.htm

I want to seperate this path into three sections,

$1 = d:\nucouk\prestage
$2 = \products\motor
$3 = index.htm

I have tried to do this using the expression below and it worked fine. But, the first part of the path is hard coded making the program less useful. I would like to make it flexible and alow the first section to change based on the directory path supplied at the command line.

$File::Find::name =~ m!^d:\\nucouk\\prestage(.*)/(.*)$!;
$virtualPath = $1 . $2;

I am new to regular expressions and have been trying many variations but just cannot seem to crack this one. Can anyone help?

Reply With Quote
  #2  
Old March 20th, 2001, 11:08 AM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

Well dealing with command line input has nothing to do with regular expressions for one thing.

There is a built in array called @ARGV has as its elements any input from the command line on a space delimited basis.

So if you had:
Code:
c:>perl script.pl foo bar

Array @ARGV would be holding 'foo' & 'bar'.

Hope that helps.
__________________
- dsb -
Perl Guy

Reply With Quote
  #3  
Old March 20th, 2001, 12:50 PM
unobserved unobserved is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Posts: 11 unobserved User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to unobserved
I'd suggest not using .* and try [\w\\]* instead.

I think .* sometimes just finds everything that you don't want it too.

and in this part
Code:
(.*)/(.*)


I don't understand what the '/' is in there for.

or alternatly, you could pull out anything after the root dir, and then split what you pulled out again to find anything after the last backslash.


Reply With Quote
  #4  
Old March 21st, 2001, 01:39 PM
mcsimon mcsimon is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2000
Posts: 179 mcsimon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 37 m 38 sec
Reputation Power: 9
Send a message via AIM to mcsimon Send a message via Yahoo to mcsimon
You could loop through the string holding the path:

$path = "path/name/till/here.html";

foreach number of slashes in $path + 1 {
$path =~ s/([^/]*)$//; # grab & remove file or dir name
$array[x] = $1;
$path =~ s/\/$//; # remove the ending slash
}

Not the prettiest code but it illustrates the point.
If you're lucky you'll end up with an array that holds the original path without the slashes.


http://www.votematic.com


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Problems seperating a path using regex


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT