in reply to filtering folder path using regex.

Your using a greedy regex '\\.+\\', which will grab as much as it can. You can either use a nongreedy version

$path =~ /^(\w):\\(.+?)\\?/;

Or better, match anything except the terminator char (\).

$path =~ /^(\w):\\([^\\]+)\\?/;

That said, you'd almost certainly be better of using the File::Spec module that is a part of the core for this sort of thing.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Replies are listed 'Best First'.
Re: Re: filtering folder path using regex.
by blackadder (Hermit) on Jul 13, 2003 at 14:30 UTC
    Thanks Sir,

    the $path =~ /^(\w):\\(^\\+)\\?/ worked for me, but what I don't understand how does it get the $2 value? There is no “.” or “\w” in the second parenthesis. Oh well, as long as it works…
      I think you were talking about this one:
      $path =~ /^(\w):\\([^\\]+)\\?/;
      In this one [\\] would match just backslashes, but with the ^, [^\\], matches all characters Except backslashes, and because of the +, it matches as much as possible up to the 1st backslash. $2 is populates, because the character class (ie. [^\\]) is in parenthesis, which happens to be the second set of them, so therefore is stored in $2
        Spot on with your explanation kind sir.
        [] and ^ means all Except backslashes,...I confused it with ^ as an anchor or start of a word boundary....I should’ve been more careful when constructing a Regex expressions!…

        Thanks for reminding me....

        & I should always keep practice coding.