$regexp =~ s/ \G ( (?:[^\\]|\\[^Q])* ) (?: \\Q (?:[^\\]|\\[^E])* (?:\\E)? )? / $1 . (defined($2) ? quotemeta($2) : '') /xge;