in reply to Moving sub-strings to the start of a line

I'm assuming your anchor format is fairly rigid. If it's not, you will need a more complicated regex.
use strict; use warnings; while (<DATA>) { @_ = (); push @_, $1 while s/(<a name=".*?">)//i; $_ = join '', @_, $_; print; } __DATA__ This is an anchor <a name="_Toc00123998">and these are another <a name +="_Toc00123999"><a name="_Toc00124000">couple. This more anchors <a name="_Toc00123998">and more anchors <a name="_To +c00124000">.