KGNickl
asked on
Perl Regular expression needed
I have a path variable. It always starts with R:\\ and is followed by one or more directories beneath. I need a regular expression that keeps the 1st folder and removes the drive ( R:\\ ) and anything that follows the first folder. So all that will be left is the first folder name. Have a couple examples below to make it clear what I'm wanting.
Example 1
$file = R:\\SomeDirectory123\\BlaB laBla\\Hel lo123\\
$folder =~ EXPRESSIONHERE that modifies $file;
Value of $folder should be: SomeDirectory123
Example 2
$file = R:\\SomeOtherDirectory321\ \
$folder =~ EXPRESSIONHERE that modifies $file;
Value of $folder should be: SomeOtherDirectory321
Example 1
$file = R:\\SomeDirectory123\\BlaB
$folder =~ EXPRESSIONHERE that modifies $file;
Value of $folder should be: SomeDirectory123
Example 2
$file = R:\\SomeOtherDirectory321\
$folder =~ EXPRESSIONHERE that modifies $file;
Value of $folder should be: SomeOtherDirectory321
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The solution by roundel35 is indeed safest and securest if you wish to restrict everything to drive R:; it will fail if the drive letter changes. The minimal matching ensures the left-side anchoring and will get the top directory on a local drive or a network share.