In the long run, the regexp method can become really tedious, or impossible to achieve, regarding to your skills. It can even cause issue in specific cases (Imagine there is a green span inside [code][/code] => error ).
A more cleaner approach would be to parse your html string(s) so you would be able to navigate through your element, as you would do in javascript/jQuery.
The are many HTML parsing library in PHP, i'm still using SimpleDomParser but you can easily grab another one.
So in your case, it would require first to extract eachs strings between "[code]" and "[/code]", parse them, search for valid elements in them, create a green span for each found then moving it inside.
Or something of this kind, regarding to your orgininal datas.
This library is not really convenient to work with, but the documentation is clear, concise, with a lot of sample.