Several interesting ideas floating around but I'd like to try one like this, jethro's being the closest to what I'd like to use. I realized my inital XML example was flawed, so let me try again with a more clear example.
<DataStore> <DataRecord> <Data>123456</Data> <Data2>654321</Data2> <Data>123456</Data> </DataRecord> <DataRecord> <Data>123456</Data> <Data>123456</Data> <Data2>123456</Data2> <Data>1234/3456</Data> <Data>123456</Data> <Data>1234/3456</Data> <Data3>123456</Data3> <Data>123456</Data> </DataRecord> <DataRecord> <Data>123456</Data> <Data>123456</Data> <Data5>123456</Data5> </DataRecord> </DataStore> # From that I want it to loop through and store each <DataRecord> ... +</DataRecord> # From then, if it matches on 4 digits followed by a forward slash I # want it to output the whole <DataRecord> to screen, not just the mat +ched lines from second filter. # For that, I've tried this example open(FILE, "< $FILE") or die "ERROR: $!"; while (<>) { if (/<DataRecord>/ ... /<\/DataRecord>/) { @cache=(); } push(@cache, $_); if (m/<Data>\d{4}\//){ print @cache; } } close (FILE); # The output of that is <Data>1234/3456</Data> <Data>1234/3456</Data> # where I would prefer to see <Data>123456</Data> <Data>123456</Data> <Data2>123456</Data2> <Data>1234/3456</Data> <Data>123456</Data> <Data>1234/3456</Data> <Data3>123456</Data3> <Data>123456</Data>
I wrote it several different ways, and either it prints every <DataRecord> or only filtered <Data> lines, neither is what I need. I want it to print the entire <DataRecord> if that record matches on the second pattern. Clearly I'm doing it wrong but I'm not seeing what, so I assume its glaringly obvious.

In reply to Re^2: Match on line, read backwards to opening xml tag then forward to closing tag by shadowfox
in thread Match on line, read backwards to opening xml tag then forward to closing tag by shadowfox

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.