I would definitely advise you to use a template module (I use Text::Template myself): it will work out-of-the-box, won't have bugs and give you way more features than what you would code yourself. Plus you would not have to craft the kind of regexp you will see below.

Of course that said, I can't resist the temptation to give you a proper regexp (all solutions so far will fail for <~hash{foo~bar}~>):

s/<~(([^~]*(~(?!>))?)*)~>/expand( $1)/g

I am totally unable to explain the regexp clearly, so try it, look at the regexp doc and above all, do yourself a favor and buy a copy of Mastering Regular Expressions in which Jeff Friedl does a great job at explaining this sort of things.

update: OK let's try to explain it anyway:

s/<~( ( [^~]* # match anything but a ~ (~(?!>))? # match a ~ NOT followed by a > # the non > char is not captured so # it is still available for matching )* # match this sequence again )/expand($1)/x;

The regexp captures all characters up to a ~
if the ~ is followed by a > it stops there
if the tilda is NOT followed by a > then the optional block is matched, the following (non >) character has not been used so the outer ()* starts with this character and starts matching again until a ~

Re-update: the expanded regexp was missing a closing paren, I fixed it


In reply to Re: Regex and HTML Question by mirod
in thread Regex and HTML Question by r.joseph

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.