in reply to Restarting counters in text

(updated) In your "if" construct, the "!=" relationship is coercing both sides to numeric. The effect on the if statement is that it is equivalent to /^(\d+)/ and ( $1 ) and $exmno=0; which is probably not what you wanted. If the line were stored in a declared variable, what you would want instead would be
if ( $variable !~ /^<exm>/ ) { $exmno = 0 );
but because the variable to match is $_, this is the default variable for the match operator and it is therefore sufficient to write:
if ( !/^<exm>/ ) { $exmno = 0 };
or even shorter is
/^<exm>/ or $exmno = 0;
Two further points are that 1) this test is anyway redundant because s// returns false for non-match and can be used directly to drive the reset to zero and 2) if the counter is zero until it encounters an exm line, and if the first exm line in a group is number 1, then the counter needs to be pre- rather than post-incremented, e.g.:
my $exmno = 0; while(<>) { s/^<exm>/'<exm num ='.++$exmno.'>'/ or $exmno = 0; print $_; }

-M

Free your mind