Hi! I have a problem and Perl Monks was highly recommended as a community that might be able to help. I'm sorry it is not an actual Perl problem but I'm hoping you will help regardless. My problem is that I need a single (one!) regular expression that I and everyone I know cannot seem to come up with. So here goes...
Sample XML: <para0 id="p00001"> <title>title</title> <warning> <?warningcaution MOD_A ### ID_1?> <para>moda id1 warning</para> </warning> <para>This is a paragraph.</para> <subpara1> <para>This is a paragraph.</para> </subpara1> <subpara1 id="sp00001"> <title>title</title> <warning> <?warningcaution MOD_B ### ID_2?> <para>modb id2 warning</para> </warning> <para>This is a paragraph.</para> </subpara1> <warning> <?warningcaution MOD_C ### ID_3?> <para>modc id3 warning</para> </warning> <caution> <?warningcaution MOD_D ### ID_4?> <para>modd id4 warning</para> </caution> </para0>
The regex needs to return the first warning element directly parented by a para0 element (no grandchildren allowed). A para0 element may parent in order: applic, title, capgrp, warning, ...(other elements)..., subpara1. Each of these children elements are optional and each may parent other elements. With that said, the applic, title, and capgrp elements should never be an ancestor of a warning element themselves. The elements after child warning elements (e.g., other elements, subpara1) may be an ancestor of a warning element.

In the sample above, my expected response is the warning element for MOD_A/ID_1. If warning MOD_A/ID_1 is removed, my expected response is no match.

The regex that I've come up with so far is shown below. I intended it to match a para0 followed by an optional applic, followed by an optional title, followed by an optional capgrp, followed by a warning. It returns my expected response for the sample above but if I remove the para0 child warning element, it matches on the warning MOD_B/ID_2 which is not what I want.

<para0[^>]*>\s*(?:<applic[^>]*>.*?</applic\s*>)?\s*(?:<title[^>]*>.*?< +/title\s*>)?\s*(?:<capgrp[^>]*>.*?</capgrp\s*>)?\s*(<warning[^>]*>\s* +.*?\s*</warning\s*>)
Help!

In reply to regex - need first child of parent by kdolan

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.