Regex regex = new Regex(@"X(-?\d+)\.(\d+)Y(-?\d+)\.(\d+).*?G00Z(-?\d+)\.(\d+).*?G00X(-?\d+)\.(\d+)Y(-?\d+)\.(\d+).*?G01Z(-?\d+)\.(\d+).*?G02X(-?\d+)\.(\d+)Y(-?\d+)\.(\d+)I(-?\d+)\.(\d+)J(-?\d+)\.(\d+)", RegexOptions.Singleline);
textBox1.Text = regex.Replace(textBox1.Text, "G5 X$1,$2 Y$3,$4\nG0 Z$5,$6\nG0 X$7,$8 Y$9,$10\nG1 Z$11,$12\nG2 X$13,$14 Y$15,$16 I$17,$18 J$19,$20");
<a href="#hello">hello</a>
<a href="site.ru?12">hello</a>
<a href="hello-1">hello</a><a href="hello-2">hello</a>
<a href="hello">hello<a href="hello-2">hello</a>
<a title="привет" href="#hello">hello</a>
<a href="test">test</a>
/{\%(.*?\%})\%}/i
/{%([^{}%]*(?:(?R)|)[^{}%]*)%}/