View Full Version : help with extracting file name from a browse box input
08-11-2002, 06:20 AM
Does anyone know how one would go about extracting the file name from a browse box input?
How would you get "filename.gif" out of that knowing everyone has a different directory structure on their computer?
I want to use a browse box as the input, and "automatically" get the file name from it.
If anyone knows that would be great I am new to Perl and am learning as I go teaching myself. Sometimes you need help from someone that actually knows what they are doing!!
08-11-2002, 10:42 PM
If $filename held the filename with the full path you could use this:
$filename =~ m/([^\/\\]+)$/;
$filename = $1;
To get the actual name bit, minus the path.
08-12-2002, 06:04 AM
Mouldy Goat thanks for the help it seems to work fine. Thanks again.
If it isn't a big long explaination could you tell me how this works?
If it is too long forget it I don't want to impose on your time.
08-13-2002, 02:53 AM
Sure I'll do it in bits for you, there's a good variety of regex bits in it..
The first line means do a match on the string $filename and save the bit it matches for later use.
This is the regex:
The parentheses (), mean save everything matched in the variable $1 so that it can be used later.
The square brackets mean a character class, i.e. a selection of characters, so that [aeiou] would match one single vowel.
A caret ^ at the very beginning of a character class means that the character class is negated, so that [^aeiou] would match anything that isn't a vowel - i.e. any one consonant.
Inside the actual character class there's basically just a forward slash and a backward slash.. but because these are both special characters they have to be preceded by a backslash themselves in order to actual match.
The + means match one or more of whatever precedes it - i.e. anything that isn't a forward or backward slash.
The $ at the end of the string means match the expression at the very end.
So all in all it means this: match one or more non-slash character at the end of the string, i.e. everything after the last slash.
I've made it sound a lot more complicated than it is.. hope I helped anyway.
08-13-2002, 02:58 AM
By the way, since you're new to Perl I should explain that regex means regular expression which means pattern matching essentially.
They might seem a little convoluted and perhaps even superfluous at first, but they're really incredibly useful once you get the hang of them. You introduce a regular expression with a =~, for example:
$phrase =~ /monkey/;
Will check to see if the variable $phrase contains the word monkey, and if it does it will return true.
More complicated regular expressions can do things like validating email addresses, checking in text to automatically detect website addresses, making user input safe from a security point of view and parsing HTML documents.
08-13-2002, 08:09 AM
I appreciate your time to explain it.
It works perfectly!!:D
08-13-2002, 10:32 PM
Regex's always give me @%$# and this is one of the best explanations I've seen yet.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.