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="" pml="xxads3" /&gt;</xhtml><xhtml>&lt;img src="" test="" test2="" pml="xxads4" /&gt; test3=""</xhtml><xhtml>&lt;p&gt &lt;img 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 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.

    If there is  no 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.

    Thursday, August 6, 2015 12:11 PM

Answers

  • Check the next expression:

    string 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="""" pml=""xxads3"" /&gt;</xhtml><xhtml>&lt;img src="""" test="""" test2="""" pml=""xxads4"" /&gt; test3=""""</xhtml>
                    <xhtml>&lt;p&gt &lt;img 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>";
    
    string pattern = @"&lt;img\s+((?!/&gt).)*(?<=\s)pml=""(?<pml>.*?)"".*?/&gt;";
    
    string output = Regex.Replace( input, pattern, "${pml}" );
    

    • Edited by Viorel_MVP Thursday, August 6, 2015 5:00 PM
    • Marked as answer by bnidv Friday, August 7, 2015 10:34 AM
    Thursday, August 6, 2015 4:59 PM

All replies

  • Check the next expression:

    string 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="""" pml=""xxads3"" /&gt;</xhtml><xhtml>&lt;img src="""" test="""" test2="""" pml=""xxads4"" /&gt; test3=""""</xhtml>
                    <xhtml>&lt;p&gt &lt;img 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>";
    
    string pattern = @"&lt;img\s+((?!/&gt).)*(?<=\s)pml=""(?<pml>.*?)"".*?/&gt;";
    
    string output = Regex.Replace( input, pattern, "${pml}" );
    

    • Edited by Viorel_MVP Thursday, August 6, 2015 5:00 PM
    • Marked as answer by bnidv Friday, August 7, 2015 10:34 AM
    Thursday, August 6, 2015 4:59 PM
  • Thanks a ton....It worked..Appreciate your valuable and quick response...
    Friday, August 7, 2015 10:38 AM
  • Hi,

            I have a small issue here.

    string 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 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>";

    sometimes there are data-pml tags in place of pml tags(as in the above input). The replace seems to have some issue. Sorry for not including in my initial question. Even the data-pml tags needed to be replaced by the content inside them.

    Desired 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>

    Thanks

    Friday, August 7, 2015 12:03 PM