Chaddeus
Chaddeus

Reputation: 13356

C# regex parse to pull photos from markdown?

Photos in markdown look like ![alt text](src). I'd like to parse a block of markdown, pulling all photo sources into an IList<string>.

I know regex is probably the answer, but it's like an ancient cryptic language to me... how would I go about parsing out all photos into an IList<string> using C#?

Example text

![](http://sphotos-b.xx.fbcdn.net/hphotos-ash3/530402_10151620341829517_1993977231_n.jpg)
When I say "Japanese-Chinese" food I'm not referring to some new type of restaurant      serving both Japanese and Chinese food. This means the restaurant serves Chinese food   prepared for Japanese tastes.

![](http://sphotos-f.ak.fbcdn.net/hphotos-ak-frc1/385269_10151620364269517_1368819859_n.jpg)
This is **subuta** -- sweet and sour pork, this one photo showing the meal set (tenshoku in Japanese). It's one of many Chinese dishes this restaurant serves... and they have **A LOT**!

Upvotes: 1

Views: 495

Answers (1)

Dave Bish
Dave Bish

Reputation: 19646

Something like this, should work:

!\[.*?\]\(.*?\)

have a play here, to see how this works:

http://tinyurl.com/qxjqhcs

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

Upvotes: 3

Related Questions