in reply to Matching a pattern in Regex

Lot's of good advice is already here. Just one more item to deal with, the OP's question at the end of his post.
Can anyone tell me what's wrong in my code?
The line
$_ =~ s/<figr n="(.+?)">(.*?)<\/figr>/<FIGIND NUM="$fno" ID="FG.$figno +">$2<\/FIGIND>/g;
should use the $fno variable, as in
$_ =~ s/<figr n="($fno)">(.*?)<\/figr>/<FIGIND NUM="$fno" ID="FG.$f +igno">$2<\/FIGIND>/g;
That will correct the repeated substitutions for the wrong number. After that, change the "if" to a "while" and the code works.

Works is good, but there are better ways. For those, see previous posts.