【问题标题】:only get the src value只获取 src 值
【发布时间】:2013-08-20 08:33:10
【问题描述】:

我在数据库中有一个总是有标签的字段。还有很多文字..

例如:

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

我只需要得到 ​​p>

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: parsing "\([^]]+)\/>" - 引用未定义的组名 img。

不知道我的代码是否正确...

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    这个问题已经被here问过了。

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

    【讨论】:

    • 如果src后面没有prop就不行了。您必须在源代码之后添加类似 ált' 或其他内容。将 "" 替换为 ""
    【解决方案2】:

    你可以试试这个正则表达式模式:-

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

    【讨论】:

    • @thormayer:- 我可以知道我的答案出了什么问题,因为它既没有被赞成也没有被接受,因为另一个答案只是我答案的复制粘贴!!!! :(
    • 我的答案不是您的副本/粘贴,而是我引用的已回答问题的副本。请不要为这样的事情发火……这是一个问答网站,而不是排行榜。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    相关资源
    最近更新 更多