#!/usr/bin/perl
 
$file = 'chapter1.txt';
my ($chapnumber) = ($file =~/chapter(\d+).txt/);
 
while (<>)
{ 
    ~s/\[(\d+)]/<a href="c${chapnumber}f$1.html">[$1]<\/a>/g; 
    print $_;
}