none
Help in building regular expression RRS feed

  • Question

  • Hi,

    Input:

    <xhtml>&lt;img src="" /&gt;</xhmtl><xhtml>&lt;img src="" pml="xxads1" /&gt;&lt;img src="" pml="xxads2" /&gt;</xhmtl><xhtml>&lt;img src="" data-pml="xxads3" /&gt;</xhtml><xhtml>&lt;img src="" test="" test2="" pml="xxads4" /&gt; test3=""</xhtml><xhtml>&lt;p&gt &lt;img data-pml="xxads5" src="" /&gt; test3="" &lt;/p&gt;</xhtml><xhtml>&lt;img alt="" src="" pml="xxads6" /&gt; test3=""</xhtml><xhtml>&lt;img src="" /&gt;&lt;img src="" pml="xxads7" /&gt;</xhmtl><xhtml>

    Output:

    <xhtml>&lt;img src="" &gt;</xhmtl><xhtml>xxads1xxads2</xhmtl><xhtml>xxads3</xhtml><xhtml>xxads4 test3=""</xhtml><xhtml>&lt;p&gt xxads5 test3="" &lt;/p&gt;</xhtml><xhtml>xxads6 test3=""</xhtml><xhtml>&lt;img src="" &gt;xxads7</xhmtl><xhtml>

    Need to replace the entire img tag with the "pml" or the "data-pml" attibute value.

    &lt;img src="" pml="xxads1" &gt; need to be replaced with xxads1.

    &lt;img  pml="xxads1" src="" &gt; need to be replaced with xxads1.

    &lt;img data-pml="xxads3" /&gt; need to be replaced with xxads3

    &lt;img src="" data-pml="xxads3" /&gt; need to be replaced with xxads3

    If there is  no pml or data-pml attribute in img tag, the img tag should be left as it is.

    Need to build a regular expression and use regex.replace to replace values wherever

    Thanks.

    Sorry for not adding the additional requirement in the other question


    • Edited by bnidv Friday, August 7, 2015 12:15 PM
    Friday, August 7, 2015 12:14 PM

Answers

  • Try adjusting the previous expression, adding “(data-)?”:

    string pattern = @"&lt;img\s+((?!/&gt).)*(?<=\s)(data-)?pml=""(?<pml>.*?)"".*?/&gt;";
    

    • Marked as answer by bnidv Monday, August 10, 2015 8:34 AM
    Friday, August 7, 2015 7:32 PM

All replies

  • Try adjusting the previous expression, adding “(data-)?”:

    string pattern = @"&lt;img\s+((?!/&gt).)*(?<=\s)(data-)?pml=""(?<pml>.*?)"".*?/&gt;";
    

    • Marked as answer by bnidv Monday, August 10, 2015 8:34 AM
    Friday, August 7, 2015 7:32 PM
  • Thank you...
    Monday, August 10, 2015 8:34 AM