4

I have in the database a field that will always have an tag. and also lots of text..

For example:

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

I need to get ONLY what between the

src

I tried :

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;
}

But I've got a parsing error :

Exception Details: System.ArgumentException: parsing "\([^]]+)\/>" - Reference to undefined group name img.

dont know though if my code is the right path...

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
thormayer
  • 1,070
  • 6
  • 28
  • 49

2 Answers2

16

This question has already been asked here.

string matchString = Regex.Match(original_text, "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value;
Community
  • 1
  • 1
Atlasmaybe
  • 1,511
  • 11
  • 26
  • it does not work if there is no prop after src. you have to add like ált' or something after source. replace "" with "" – Baqer Naqvi Mar 24 '17 at 16:19
1

You can try this Regex pattern:-

string pattern= Regex.Match(path, "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • @thormayer:- May I know what was wrong in my answer as it was neither upvoted nor accepted as the other answer was just a copy paste of my answer!!!! :( – Rahul Tripathi Aug 20 '13 at 08:55
  • My answer is not a copy/paste of yours, but a copy of an already answered question that I quoted. Please don't rage for something like this... It's a Q/A website, not a leaderboard. – Atlasmaybe Aug 21 '13 at 09:01