1
    <img src=\"%24FILEBASE%moga.jpg\" alt=\"moga.jpg\" width=\"882\" height=\"324\">

I need this value: "moga.jpg" Note: There is no closing tag for this. So, I was not able to successfully come up with a linq expression. Is there a cleaner way using linq or regex or something else?

Barmar
  • 741,623
  • 53
  • 500
  • 612

2 Answers2

1

Use HtmlAgilityPack to parse HTML. Don't use LINQ-To-XML or regex to parse HTML.

var document = new HtmlDocument();
document.LoadHtml(/* your html string */);

var navigator = (HtmlNodeNavigator)document.CreateNavigator();
var value = navigator.SelectSingleNode("//img/@src").Value;
Community
  • 1
  • 1
rexcfnghk
  • 14,435
  • 1
  • 30
  • 57
0

if yout HTML is perfectly formatted, you can match it using regular expressions if you want! No problem. But @rexcfnghk advice is correct in the pratical world...

If someone wants to use Regular Expression... try this example! You could explore the collection better to check if the content is there, of course... just explore the MatchCollection class.

string htmlNode = "<img src=\"%24FILEBASE%moga.jpg\" alt=\"moga.jpg\" width=\"882\" height=\"324\">";
MatchCollection collection = Regex.Matches(htmlNode, "src=\\\"%24FILEBASE%(.*?)\\\"", RegexOptions.IgnoreCase);
string result = collection[0].Groups[1].Value;
Wagner Patriota
  • 5,494
  • 26
  • 49