only get the src value

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="https://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…

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This question has already been asked here.

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

Method 2

You can try this Regex pattern:-

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x