Hello monks,

I have been staring at this problem all day, and my error refuses to get up and wave its arms at me.

I'm writing a perl script to munge an XML DTD (for reasons we needn't go into). One of the script's tasks is to find parameter entity definitions that are empty and remove the references to those entities from any element definitions.

# $entity = the name of the entity (already initialized) # $text = the full DTD (already initialized) # comment out the empty entity definition # e.g., <!ENTITY % foo " " > # so far this section works fine, wrapping the empty entity definition + in comment tags if ( $text =~ /(<!ENTITY\s+%\s+$entity\s+\"\s+\"\s+>)/ ) { print "Commenting out empty entity '$entity'\n"; my $entity_def = $1; $text =~ s/$entity_def/<!--\n$entity_def\n-->/; } # here's the problem: I want to find # <!ELEMENT bar (#PCDATA %foo;)* > # and remove the "%foo;" if ( $text =~ /(<!ELEMENT\s+(\S+)\s+\(.*?$entity.+?\).+?>)/ ) { my $element_def = $1; my $element_name = $2; print "Found empty entity '$entity' in $element_name " . "content model: $element_def\n"; my $new_element_def = $element_def; $new_element_def =~ s/\|?\s*%$entity;//; print "Looking for $element_def\n"; # this NEVER PRINTS--WHY? print "Found it!\n" if $text =~ /$element_def/; }

I always manage to capture the element definition in $element_def, but my final regex never works, and I can't figure out why.


In reply to Regex problem by mboudreau

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.