I'm guessing your real intention is to check for well-formedness. The question also implies you're doing XML parsing in a fragile way elsewhere. If I'm right, the following might be a good start to doing things in a way that's more bomb-proof and easier to extend. See XML::LibXML for more.

use XML::LibXML; local $/ = "::FILE::"; my $parser = XML::LibXML->new(); # $parser->recover(1); <-- turn on to "save" many bad docs. while ( my $xml = <DATA> ) { chomp($xml); my $doc = eval { $parser->parse_string($xml) }; if ( $doc ) { print "File $. is valid.\n"; # Do whatever you want with your valid $doc here. } else { print "File $. is NOT valid.\n"; # Deal with bad docs here... } } __DATA__ <order> <customer> <name>Coyote, Ltd.</name> <shipping_info> <address>1313 Desert Road</address> <city>Nowheresville</city> <state>AZ</state> <zip>90210</zip> </shipping_info> </customer> <item> <product id="1111">Acme Rocket Jet Pack</product> <quantity type="each">1</quantity> </item> <item> <product id="2222">Roadrunner Chow</product> <quantity type="bag">10</quantity> </item> </order> ::FILE:: <order> <customer> <name>Coyote, Ltd.</name> <shipping_info> <address>1313 Desert Road</address> <city>Nowheresville</city> <state>AZ</state> <zip>90210</zip> </shipping_info> </customer> <item> <product id="1111">Acme Rocket Jet Pack</product> <quantity type="each">1</quantity> </item> <item> <product id="2222">Roadrunner Chow</product> <quantity type="bag">10</quantity> </item>

In reply to Re: Regular Expression XML Searching Help by Your Mother
in thread Regular Expression XML Searching Help by Anonymous Monk

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.