Link to home
Start Free TrialLog in
Avatar of KGNickl
KGNicklFlag for United States of America

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\\BlaBlaBla\\Hello123\\
$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
Avatar of parparov
parparov
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.