First: To work with XML::LibXML::Reader, your XML document has to be valid. Your example has a bad entity (as has been already pointed out by LanX), also the named entities are not declared.

Without any attempt to make it pretty, here's a hack with your (corrected) example text. It shows a minimal context around each parenthesis found and indents according to level.

use 5.032; use warnings; use XML::LibXML::Reader qw( :types ); use open ':std', OUT => ':encoding(UTF-8)'; my $data = \*DATA; my $reader = XML::LibXML::Reader->new( IO => $data ); my $paren_level = 0; while ($reader->read) { next unless $reader->nodeType == XML_READER_TYPE_TEXT; my $text = $reader->value; while ($text =~ /([^() ]*\s*)([()])(\s*[^() ]*)/g) { if ($2 eq '(') { say " " x ($paren_level++ * 4), "opening: '$&'"; } else { if ($paren_level < 1) { say "non-matching: '$&'"; } else { say " " x (--$paren_level * 4), "closing: '$&'"; } } } } if ($paren_level) { say "At the end, $paren_level parentheses were left unclosed."; } __DATA__ <?xml version="1.0"?> <!DOCTYPE rn [ <!ENTITY auml "ä"> <!ENTITY Auml "Ä"> <!ENTITY ouml "ö"> <!ENTITY Ouml "Ö"> <!ENTITY uuml "ü"> <!ENTITY Uuml "Ü"> <!ENTITY sect "§"> <!ENTITY emsp14 "&#8197;"> <!ENTITY ldquor "„"> <!ENTITY rdquor "”"> ]> <rn> <rnnum>52</rnnum> <p>In Anlehnung an den Ansatz von Tokio 2013 <emph>definiert</emph> &s +ect;&emsp14;1 II&emsp14;XXX die nachhaltige Bewirtschaftung als eine +Bewirtschaftung, die die sozialen und wirtschaftlichen Anspr&uuml;che + an den Boden mit seinen &ouml;kologischen Funktionen in Einklang bri +ngt. Es soll eine gleichberechtigte Berücksichtigung von &ouml;kologi +schen, &ouml;konomischen und sozialen Aspekten (&ldquor;Bedarfstripel +&rdquor;<fn id="xxx"> <p>Mayer, JJV 2022, 28, 29.</p> </fn>) f&uuml;r die Nutzung angestrebt werden. Ziel ist ein Ausgleich +der drei &ldquor;S&auml;ulen&rdquor;, die prinzipiell gleichwertig si +nd. <fn id="yyy"> <p>Reiter, in: BoGB, &Ouml;ffentliche Regeln, &sect;&emsp14;4 Rn.&emsp +14;6&emsp14;f.; Mayer, NatBl. 1996, 1082, 1083.</p> </fn> Dabei muss allerdings der Grundgedanke der St&auml;rkung der &ou +ml;kologischen Funktionen im Sinne der Ressourcenschonung ber&uuml;ck +sichtigt werden. Die &ouml;kologischen Funktionen m&uuml;ssen mit den + wirtschaftlichen und sozialen Interessen in Einklang gebracht werden +.<fn id="zzz"> <p>So auch in seiner Einleitung der Beschluss der 72. ROK vom 27.&emsp +14;06. 2017, der in (&ldquor;Leitbilder und Handlungsstrategien f&uum +l;r die Bodennutzung (in l&auml;ndlichen Gebieten)&rdquor;) sehr gut +die aktuellen Herausforderungen beschreibt, die unterschiedlichen Asp +ekte nach dem Topziel der Nachhaltigkeit strukturiert und sie mit bei +spielhaften Handlungsans&auml;tzen versieht.</p> </fn> Wenn die &Ouml;kologie von vornherein gegen&uuml;ber den wirtsch +aftlichen und sozialen Anspr&uuml;chen zur&uuml;cktritt, verliert die + Nachhaltigkeit ihre Orientierungsfunktion, und die Begrifflichkeit w +ird ad absurdum gef&uuml;hrt.</p> </rn>
This example has no non-matching parentheses, the output reads:
opening: 'Aspekten („Bedarfstripel”' closing: ') für' opening: 'in („Leitbilder' opening: 'Bodennutzung (in' closing: 'Gebieten)”' closing: ') sehr'

In reply to Re^3: Finding parentheses that won't match by haj
in thread Finding parentheses that won't match by LexPl

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.