在bbcode C#中获取First Img链接

时间:2014-12-28 16:52:05

标签: c# asp.net-mvc entity-framework bbcode

如何在c#

中获取bbcode文本中的第一个图像墨水

我将此代码用于html,但如何为bbcode

执行此操作
HtmlDocument document = new HtmlDocument();
document.LoadHtml(Model.TextSujet);
var imageUrl = (from image in document.DocumentNode.Descendants("img")
                where !String.IsNullOrEmpty(image.GetAttributeValue("src", null))
                select image.Attributes["src"].Value).FirstOrDefault();

1 个答案:

答案 0 :(得分:0)

我建议使用正则表达式

        var regex = new System.Text.RegularExpressions.Regex(@"\[img[^\]]*\]([^\[]*)");

        string text = @"Including an image
[img]http://www.bbcode.org/images/lubeck_small.jpg[/img]
Resizing the image
[img=100x50]http://www.bbcode.org/images/lubeck_small.jpg[/img]
Making the image clickable (in this case linking to the original image)
[url=http://www.bbcode.org/images/lubeck.jpg][img]http://www.bbcode.org/images/lubeck_small.jpg[/img][/url]
Resizing and adding meta information for the image";

        var match = regex.Match(text);

        if (match.Success)
        {
            Console.WriteLine(match.Groups[1]);
        }