Excuse the dodgy title, I really couldn't think how to describe the problem very accurately.

So basically what I am trying to do, is to modify the source of the html document to add some special links (they are actually used to allow easy translation of a web page in context)

I think this is easier explained in a few examples, so lets take the easy one as follows, lets say I have this text in the html source

<!-- headerStandard.siteFeedbackLink.label --!>Feedback<!-- headerStan +dard.siteFeedbackLink.label --!>

And lets say that I have a hash such as

%data = ('headerStandard.siteFeedbackLink.label' => 'Feedback Translat +ed');

So the html comments are used to map the key to the content and are available for me in the html. I want to replace whats in between the tags with the data equivalent AND add a special link AFTER it as so above example becomes...

Feedback Translated<a href='foo'>foo</a>
So this example is easy, I just regex between the lines such as
my $heading = 'headerStandard.siteFeedbackLink.label'; $wp =~ s/\<\!-- $heading --\!>(.*)\<\!-- $heading --\!>/$data{$hea +ding}\<a href="foo"\>foo\</a>/gm;

So no problem, however my slow working brain needs some assistance with dealing when the contents I want to replace are nested inside tags already, for instance lets say the text is inside a <a href="bar"> such as

<a href="http://localhost:8585/deals/dealshome"><!-- headerStandard.de +alsTab.label --!>Deals<!-- headerStandard.dealsTab.label --!></a>
So i would want the text to be replaced inside the a tag, but the link to be outside the current href such as follows
<a href="http://localhost:8585/deals/dealshome">Deals Modified</a><a h +ref="foo">foo</a>
or inside alt tags for a image
<a href="bar"><img src="http://localhost" alt=" <!-- adTags.AdvertisementAltText --!>Advertisement<!-- adTags.Advertis +ementAltText --!> " /></a>

Again would want the text to be replaced between the comments, but add the link outside of the original a tag.

This scenario also applies to text within textfield, textareas, img tags etc etc.... SO I guess can anyone suggest a easy way to achieve what I am doing (replace the text within the comments and add the link outside any nested tags).... My regex skills are not particularly great but I am sure some of you may be able to assist or offer me a better way of doing this ?


In reply to Regex within html by ropey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.