http://www.perlmonks.org?node_id=785837


in reply to conditional statement in substitution expression

s/\*\*([^\*]+)\*\*\s\,\s\*\*([^\*]+)\*\*(\s\,)?/(grep {index($_,'_') +>=0} $1,$2)?"**$1_($2)**":$&/ge;
This incurs the performance penalty for using $&, so you might prefer
while(<DATA>) { s/(\*\*([^\*]+)\*\*\s\,\s\*\*([^\*]+)\*\*(\s\,)?)/(grep {index($_,'_ +')>=0} $2,$3)?"**$2_($3)**":$1/ge; print; }

Caution: Contents may have been coded under pressure.