in reply to RegEx filter \s ! between labels, part 2

Ok, here it goes with these specifications:

%Marks = ( STARTPRESERVE => 'STOPPRESERVE', BEGIN => 'END' ); $MarkOptions = join("|",keys %Marks); $text =~ s/ \s+ | # spaces, or: ( # begin $1 ($MarkOptions)(\d?) # $2=START + $3=optional_number .*? # anything $Mark{\2}\3 # $Mark{START} + optional_number ) # end $1 /${ [$1,' '] }[ ! $1 ]/sgx; # [$1,' '] is an unnamed array. # ! $1 is: # 0 if $1 exists; # 1 if spaces were found.

Note: you may add \b to the markers if you feel that you need it.
I kept the optional number after the marker, you may remove it if you don't need it.