Parse html via RegularExpression and place results in Array


/ Published in: C#
Save to your folder(s)

In this case I'm looking for image sources in html and placing them in array.


Copy this code and paste it in your HTML
  1. using System.Text.RegularExpressions;
  2.  
  3. //looking for photo paths with REGX-a
  4. string nl = // in this case page html.
  5. MatchCollection mc = null;
  6. string sRegExp = "src=(?:\"|\')?(?<imgSrc>[^>]*[^/].(?:jpg|bmp|gif|png))(?:\"|\')?";
  7. mc = Regex.Matches(nl, sRegExp);
  8.  
  9. string[] path;
  10. for (int j = 0; j < mc.Count; j++)
  11. {
  12. path = mc[j].Value.Split('"');
  13. Response.Write(path[1].ToString().Trim() + "<br />");
  14. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.