in reply to Re: Extracting file name from path
in thread Extracting file name from path

Actually, a dot star can be quite useful, and is considerably faster than using the non-greedy version. In this example, using dot star makes it easy, e.g.,
/^.*\\(.*)/
That will extract all the information after the last \. Using the .*? version it would have to be:
/.*?\\(?!.*\\)$/
This would aslo make it a lot slower.

However, trying to find the last \, like in this example, could probably be made simpler by a call to reverse. Though, in the example of extracting a filename, definately you should use a module like File::Basename, as aforementioned. This way it will be portable accross systems, and it is tested for errors, so it will save you valuable time trying to fix a regex that has something wrong with it(and errors always slip in, as do special cases).

Replies are listed 'Best First'.
Re: Re: Re: Extracting file name from path
by srawls (Friar) on Jun 15, 2001 at 05:21 UTC
    Good advice dimmesdale, same with all the rest. But one point:

    However, trying to find the last \, like in this example, could probably be made simpler by a call to reverse

    Close, but why not rindex. Take this example:

    $file = substr($path, 1 + rindex($path,'\\'));
    It finds the last \ in the string and asigns everything after that.

    That said, I would definately use something like File::Basename if you plan on using this script more than once.

    The 15 year old, freshman programmer,
    Stephen Rawls