Apalabrados
Apalabrados

Reputation: 1146

Regex to extract content

I do not know how to use regex in c#. I'm confused. Here is the URL where i want to exact the id:

https://j-ec.static.com/images/385/3858715.jpg

I just want to get the bolded number.

The regex I try to use is:

Match thumb_id = Regex.Match(url, @"\/(?)\.jpg");

What's wrong?

Any help?

Upvotes: 0

Views: 57

Answers (3)

Eser
Eser

Reputation: 12546

It is an url. You don't need regex for this.

var url = "https://j-ec.static.com/images/385/3858715.jpg";

var id = Path.GetFileNameWithoutExtension(url);

Upvotes: 3

BillV
BillV

Reputation: 61

You could always do it without Regex:

url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));

Upvotes: 0

napi15
napi15

Reputation: 2402

Use this instead :

Match thumb_id = Regex.Match(url, "http://(\\S+?)\\.(jpg)");

So you have :

  foreach (Match m in Regex.Matches(s, "http://(\\S+?)\\.(jpg)"))
        {
            Console.WriteLine(m.Groups[1].Value);
        }

Upvotes: 0

Related Questions