locked
Get all src attribute values of img tag RRS feed

  • Question

  • Hi ,

    I have some data which is in html markup .

    I need to get all the some text of src attribute value of img tag.

    for e.g., 

    <p>hello World </p><img src="OpenFile.aspx?Id=25" alt="" />

    this is my first post.

    <img alt=""  src="OpenFile.aspx?Id=43" />

    Need to get Id value of src tags which has the same format as mentioned above.

    Here I need 25, 43 .

    Any help would be appreciated.

    Thanks


    Monday, June 20, 2011 6:57 AM

Answers

  • Hi,

    you always have to escape your strings (nearly everywhere) - but to have a chance to say how you have to do that, you have to provide at least the information, where you want it to do.

    Is it a c# string, use e.g. a @"verbatim"-string and double every quotation mark

    @"(?<=<img src=""OpenFile\.aspx\?Id=)\d+"

    VB.NET: Same as c#, but without @ at the beginning.

    Edit: There was a mistake in the pattern, too...

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Edited by WolfgangKluge Monday, June 20, 2011 12:03 PM <= instead of =
    • Marked as answer by Paul Zhou Monday, June 27, 2011 8:00 AM
    Monday, June 20, 2011 12:00 PM

All replies

  • Hi,

    maybe

    (?<=<img src="OpenFile\.aspx\?Id=)\d+

    but I recommend to use HTML Agility Pack to extract the src-property of all img-tags and then use e.g.

    (?<=\bId=)\w+

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de

    • Edited by WolfgangKluge Monday, June 20, 2011 12:02 PM
    • Proposed as answer by xalnix Tuesday, June 21, 2011 1:42 PM
    Monday, June 20, 2011 8:48 AM
  • Hi ,

     

     

    The above regex did not work. there is a compilation error.

    May  be meta characters need to be replaced with unicode/ascii format

    like "  <=> \x22 

          . <=> \x2e

          ? <=> \x3f

     

    I've replaced with unicode but even then throws error or match not found


    Monday, June 20, 2011 9:54 AM
  • Hi,

    you always have to escape your strings (nearly everywhere) - but to have a chance to say how you have to do that, you have to provide at least the information, where you want it to do.

    Is it a c# string, use e.g. a @"verbatim"-string and double every quotation mark

    @"(?<=<img src=""OpenFile\.aspx\?Id=)\d+"

    VB.NET: Same as c#, but without @ at the beginning.

    Edit: There was a mistake in the pattern, too...

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Edited by WolfgangKluge Monday, June 20, 2011 12:03 PM <= instead of =
    • Marked as answer by Paul Zhou Monday, June 27, 2011 8:00 AM
    Monday, June 20, 2011 12:00 PM