in reply to Restarting counters in text

And a one-liner version is
$exmno=0 unless s/<exm>/'<exm num="'.++$exmno.'">'/egs;
Also note that in the OP, as here, the "++" goes before the "$exmno", otherwise you get
<exm num="0">This is example 1
instead of
<exm num="1">This is example 1

I assume you want the numbers to match.