robert
robert

Reputation: 321

regular expression to get the filename from urls

if i have a textbox with severals urls

rapidshare.com/files/379028801/foo.bar.HDTV.XviD-LOL.avi

rapidshare.com/files/379182651/foo.bar.720p.HDTV.X264-DIMENSION.mkv

rapidshare.com/files/379180004/foo.bar.720p.HDTV.X264-DIMENSION.part1.rar rapidshare.com/files/379180444/foo.bar.720p.HDTV.X264-DIMENSION.part2.rar rapidshare.com/files/379181251/foo.bar.720p.HDTV.X264-DIMENSION.part3.rar rapidshare.com/files/379181743/foo.bar.720p.HDTV.X264-DIMENSION.part4.rar

i need a the files name and its urls

textbox2.text =

foo.bar.HDTV.XviD-LOL.avi

from here
rapidshare.com/files/379028801/foo.bar.HDTV.XviD-LOL.avi

foo.bar.720p.HDTV.X264-DIMENSION.part1.rar
from here
rapidshare.com/files/379180004/foo.bar.720p.HDTV.X264-DIMENSION.part1.rar

and so on

Thanks :)

Upvotes: 0

Views: 1777

Answers (4)

vlood
vlood

Reputation: 927

Well, can't ya just get the string that follows the last / ?

Just get the substring with the beginning "LastIndexOf("/") + 1" and you'll have it, skipping all the regex stuff. It's a single-line solution and it's quicker!

It's something like that:

String filename = ulr.Substring(url.LastIndexOf("/") + 1);

Cheers,

vloo

Upvotes: 2

Computerish
Computerish

Reputation: 9591

You should be able to search for something like this regex and replace it with "":

rapidshare.com/files/[0-9]*/

For example, in JavaScript:

var fileName = fullUrl.replace(/rapidshare.com\/files\/[0-9]*\//, "");

Note that the \/ escapes the / which would, in JavaScript, otherwise end the regular expression.

Upvotes: 0

theraneman
theraneman

Reputation: 1630

I could use this regex for getting the file name,

rapidshare.com/files/[0-9]*/(?<name>.*)$

The C# code would look like,

                string text= textbox1.Text;
                Regex regex = new Regex(@"rapidshare.com/files/[0-9]*/(?<name>.*)$");
                MatchCollection matches = regex.Matches(text);
                if (matches.Count > 0)
                {
                    string fileName = (from Match match in matches
                                       where match.Success
                                       select match.Groups["name"].Value.Trim());
                }

Upvotes: 1

josephj1989
josephj1989

Reputation: 9709

Something like this should do it

String flnam = Regex.Match("rapidshare.com/files/379028801/Fringe.S02E19.HDTV.XviD-LOL.avi",
                @".*\/([^/]*)$").Groups[1].Value.ToString();
Console.WriteLine(flnam);

Upvotes: 1

Related Questions