Hey monks, have a regex problem that I'm hoping you can help with.

First off, disclaimer, the reason I am not using an html parser is the format I am going to is not very synonomous with html converters. I'm working with a very small subset, so I'm hoping to bang this out with regexes instead of a full-blown html parser.

Here's the code...
$foo = "<italic>Here's a <bold>larger<normal> paragraph, <italic>where + I'm<norma l> going to <bold>bold some <italic>"; print "\nfoo before:\n$foo\n\n"; #foo.gsub!(/(?<=<italic>)(?<!<normal>)(.*?)<bold>/, '\1<bold-italic>') $foo =~ s/(?<=<italic>)(?<!<normal>)(.*?)<bold>/\1<bold-italic>/g; print "foo after:\n$foo\n";
Here's the output I am getting...
# Output is... # <italic>Here's a <bold-italic>larger<normal> paragraph, <italic>wher +e I'm<normal> going to <bold-italic>bold some <italic>
Notice the second <bold> is being replaced with <bold-italic>. By the regex (at least I think I have the regex right) the second bold *should not* be replaced since I perform a look-behind for <normal>. If <normal> is between the <italic> and the <bold>, then the <bold> should be left alone. At least this is what I am trying to get at.

Here what I would like to see...
# However, should be... # <italic>Here's a <bold-italic>larger<normal> paragraph, <italic>wher +e I'm<normal> going to <bold>bold some <italic>
Notice the second <bold> is not replaced.

I'm confused as to what is wrong with my regex.

Thanks again Monks for all your help.

In reply to Regex look-behind problem. by the_0ne

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.