$newtext = "some new text"; $spantext =~ s/(\]+\>)[^<]+(<.*>)/$1${newtext}$2/i; #assuming there's no tags between the spans...