kdolan has asked for the wisdom of the Perl Monks concerning the following question:
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.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>
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.
Help!<para0[^>]*>\s*(?:<applic[^>]*>.*?</applic\s*>)?\s*(?:<title[^>]*>.*?< +/title\s*>)?\s*(?:<capgrp[^>]*>.*?</capgrp\s*>)?\s*(<warning[^>]*>\s* +.*?\s*</warning\s*>)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regex - need first child of parent
by talexb (Chancellor) on Mar 11, 2009 at 15:52 UTC | |
|
Re: regex - need first child of parent
by kennethk (Abbot) on Mar 11, 2009 at 16:21 UTC | |
by kdolan (Initiate) on Mar 11, 2009 at 18:37 UTC | |
|
Re: regex - need first child of parent
by afoken (Chancellor) on Mar 11, 2009 at 15:54 UTC | |
|
Re: regex - need first child of parent
by ikegami (Patriarch) on Mar 11, 2009 at 16:08 UTC | |
|
Re: regex - need first child of parent
by ig (Vicar) on Mar 12, 2009 at 17:52 UTC |