You cannot declare the two @regexes arrays in different scopes and then use them later. A variable declared with my only lasts up to the end of the enclosing block.

You can declare the variable before the condition and only populate it depending on it:

my @regexes; if ($param eq 'mod') { @regexes = ... } else { @regexes = ... }
or you can use the conditional operator (the "ternary"), shown below.

To match the entities involved in ;\s&, you need to extend the regex:

&[^;]+;\s&[^;]+;
i.e. an ampersand, not semicolon at least once, semicolon, space, and an entity again.

Here's a complete example. Running it with script.pl 1.xml searches for the original regexes, specifyin any true parameter (e.g. script.pl 1.xml 1) after the file uses the modified regexes. I also added the grouping parentheses to the matching and $1 to the output to show which part of the string actually matched the regex.

#!/usr/bin/perl use warnings; use strict; my ($infile, $param) = @ARGV; my @regexes = $param ? (qr/&[^;]+;\s&[^;]+;/) : (qr/&ndash;&sect;/, qr/&ndash;&Uuml;/, qr/&szlig;&sect;/); open my $in, '<', $infile or die "Cannot open $infile for reading: $!" +; my $xml; { local $/; $xml = <$in>; } open my $out, '>', 'pairs.txt' or die $!; print {$out} "Find pair of entities without/with separating space\n\ni +nput file: "; print {$out} "$infile"; print {$out} "\n====================================================== +==================\n\n"; for my $i (0 .. $#regexes) { my $regex = $regexes[$i]; $regex =~ s/^\(\?\^://; $regex =~ s/\)$//; print {$out} "$regex: $1\n" while $xml =~ /($regex)/g; } close $in; close $out;
Update: Fixed formatting, thanks Discipulus.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re: Output minimal occurrences of matching regex(es) by choroba
in thread Output minimal occurrences of matching regex(es) 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.