use strict; use warnings; my $data = do{local $/; }; my $i = 1; while ($data =~ s/((?:(?:(?!<\/FN$i>).)*)<\/FN$i>)\n*//gsi){ my $foot = $1; $data=~ s//$foot/gi; $i++; } print $data; __DATA__ This is my text. This is my text This is my text. This is my text. This is my text This is my text. This is my text, this is my text This is my text. This is my text, this is my text This is my text. This is my text, this is my text This is my text. Footnote1 Footnote2 Footnote3 Footnote4 Footnote5 Output: ------- This is my textFootnote1. This is my text This is my text. This is my textFootnote2. This is my text This is my text. This is my textFootnote3, this is my text This is my text. This is my textFootnote4, this is my text This is my text. This is my textFootnote5, this is my text This is my text.