Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression help 1

Status
Not open for further replies.

mevasquez

Programmer
Aug 26, 2003
75
US
I have a string that contains a URL and I want to be able to remove everything in the URL except for the file name and assign this to a variable.

For example:
$URL = "or
$URL = "
There could be more than two subdirectories.

I need to remove all except filename.ext and assign this value to a variable called $filename.

I have been trying to use regular expression substitute to accomplish this but I am fairly new with regular expressions. Any help would be appreciated.

If using a regular expression to do this, could you explain so I could learn what is transpiring.

TIA,

Mike V
 
Try this:

Code:
my $url = "[URL unfurl="true"]http://somedomain.com/somedir/somedir/file.ext";[/URL]

#split on / and only grab the last element
my $filename = (split /\//, $url)[-1];

- Rieekan
 
Or, if you really want to do it with a regex, though Rieekan's method is the way I'd go, you could use something like:
Code:
my $url = "[URL unfurl="true"]http://domainname.net/dir/subdir/subdir2/filename.ext";[/URL]
$url =~ m/
        .+\/        # Match as many characters as possible to last slash
        ([\w.]+)    # Grab all the word and . chars and put them in $1
        /x;         # Modifier allows for inline comments/whitespace

my $filename = $1;

I included comments for you, since you asked for them. You could modify it a bit if you wanted to use a substitution, but a match will work just fine.
 
Won't that fail if the file has non-word characters (eg numbers or percent signs)? I would say:

$url =~ m|/([^/]*)$|;

#ie all the non-slash characters after the last slash up to the end of the string

$filename = $1;


Dom
 
I am using Rieekan's suggestion. It seems to work. I do have a question regarding regarding the reqular expression suggestions.

Where does $1 come from? Where is it assigned value?

Thanks,

Mike

 
$1..$n are assigned by the regex itself. Every instance of a pair of parenthesis () is assigned to $n in the order that they appear in the regex. In this case, the part of the string that matches this ([\w.]+) will be assigned to $1 since this is the first (and only) pair of parens.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Another way with regular expression:
Code:
($filename = $url) =~ s{.*/}{};
# set filename to url
# and/then supress all chars before last / in filename

--------------------

Denis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top