Miguel Moura
Miguel Moura

Reputation: 39374

Extract images urls from markdown content

I have markdown content which contains images like:

#this is markdown content

![image alt text](https://via.placeholder.com/200)

Remaining content ...

Using C# I need to get the urls of all images in the markdown content.

In previous content I would get https://via.placeholder.com/200.

I am getting matches of ![image alt text](https://via.placeholder.com/200) using:

var matches = new Regex(@"!\[.*?\]\(.*?\)")
                .Matches(content).Cast<Match>()
                .Select(m => m.Value)
                .ToList();

But I am not sure how to get the urls of images ...

Upvotes: 1

Views: 612

Answers (1)

bananocko
bananocko

Reputation: 26

As mentioned by user @Evk you can capture the url in a group. The code to access the url without a named group would be:

var matches = new Regex(@"!\[.*?\]\((.*?)\)")
   .Matches(text);

if (!matches.Any())
    return;

var url = matches[0].Groups[1].Value;

and with a named Group:

var matches = new Regex(@"!\[.*?\]\((?<url>.*?)\)")
   .Matches(text);

if (!matches.Any())
    return;

var url = matches[0].Groups["url"].Value;

Upvotes: 1

Related Questions