|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Problems seperating a path using regex |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|