只获取src值

本文关键字:src 获取 | 更新日期: 2023-09-27 18:28:27

我在数据库中有一个字段,它总是有一个标记。还有很多文字。。

例如:

Hey there.. whats up?
<img src="http://cdn.theatlantic.com/static/infocus/ngpc112812/s_n01_nursingm.jpg" alt="" />
.. and this is my photo!..

我只需要在之间得到什么

src

我试过了:

public string LinkPicCorrect(string path)
{
    //string input = "[img]http://imagesource.com[/img]";
    string pattern = @"'<img>([^']]+)''/>";
    string result = Regex.Replace(path, pattern, m =>
    {
        var url = m.Groups[1].Value;
        // do something with url here
        // return the replace value
        return @"<img src=""" + url + @""" border=""0"" />";
    },
        RegexOptions.IgnoreCase);
    result = "<img src='" + result + "'/>";
    return result;
}

但我有一个解析错误:

异常详细信息:System.ArgumentException:正在解析"''([^]+)''/>"-引用未定义的组名img。

但不知道我的代码是否正确。。。

只获取src值

这里已经提出了这个问题。

string matchString = Regex.Match(original_text, "<img.+?src=['"'](.+?)['"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value;

您可以尝试以下Regex模式:-

string pattern= Regex.Match(path, "<img.+?src=['"'](.+?)['"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value;